Open In App

How To Break Up A Comma Separated String In Pandas Column

Pandas library is a Python library which is used to perform data manipulation and analysis. It offers various 2D data structures and methods to work with tables. Some times, the entire data can be in the format of string, which needed to be broken down in-order to organize the information in the pandas data structures. In this article, let us understand how to break a comma separated string in a pandas column along with different possible approaches.

Break Up A Comma Separated String In Pandas Column

Using str.split()

let us understand the requirements for this approach:



Requirements:




import pandas as pd
# Example DataFrame
data = {'Category': ['Fruits', 'Vegetables', 'Dairy'],
        'Contains': ['Apple,Orange,Banana', 'Carrot,Potato,Tomato,Cucumber', 'Milk,Cheese,Yogurt']}
df = pd.DataFrame(data)
 
# Split the 'Items_string' column by commas and create a new column 'Items_list'
df['Contains_list'] = df['Contains'].str.split(',')
 
# Display the DataFrame
print(df)

Output:

     Category                       Contains  \
0 Fruits Apple,Orange,Banana
1 Vegetables Carrot,Potato,Tomato,Cucumber
2 Dairy Milk,Cheese,Yogurt
Contains_list
0 [Apple, Orange, Banana]
1 [Carrot, Potato, Tomato, Cucumber]
2 [Milk, Cheese, Yogurt]

In the above example, we have imported the pandas library.



Using str.split() with the expand

We will again create a dataframe and use “expand=True” parameter.




import pandas as pd
 
# Example DataFrame
data = {'Category': ['Fruits', 'Vegetables', 'Dairy'],
        'Contains': ['Apple,Orange,Banana', 'Carrot,Potato,Tomato,Cucumber', 'Milk,Cheese,Yogurt']}
df = pd.DataFrame(data)
 
# Split the 'Contains' column by commas and expand it into separate columns
df[['Item1', 'Item2', 'Item3', 'Item4']] = df['Contains'].str.split(',', expand=True)
 
# Display the modified DataFrame
print(df)

Output:

     Category                       Contains   Item1   Item2   Item3     Item4
0 Fruits Apple,Orange,Banana Apple Orange Banana None
1 Vegetables Carrot,Potato,Tomato,Cucumber Carrot Potato Tomato Cucumber
2 Dairy Milk,Cheese,Yogurt Milk Cheese Yogurt None

Article Tags :