How to Split a Column in a Python DataFrame by Comma?

Estimated read time 1 min read

To split a column in a Python DataFrame by comma, you can use the str.split() method provided by pandas. Here’s an example:

import pandas as pd

# Create a DataFrame with a column containing comma-separated values
data = {'Name': ['John,Smith', 'Jane,Doe', 'Bob,Johnson']}
df = pd.DataFrame(data)

# Split the Name column by comma
df[['First Name', 'Last Name']] = df['Name'].str.split(',', expand=True)

# Drop the original Name column
df.drop('Name', axis=1, inplace=True)

# Print the resulting DataFrame
print(df)

In this code, the str.split() method is used to split the Name column of the DataFrame by comma. The expand=True parameter causes the resulting split strings to be placed in separate columns. The resulting columns are named First Name and Last Name using the [['First Name', 'Last Name']] syntax, and added to the DataFrame using df[['First Name', 'Last Name']] =. The original Name column is then dropped using the drop() method, and the resulting DataFrame is printed to the console.

This will output:

  First Name Last Name
0       John     Smith
1       Jane       Doe
2        Bob   Johnson

You can customize the code by modifying the column name, separator character, or output column names based on your specific needs.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply