I have column with lists in a dataframe. I want to just remove the square brackets:
| Fruits |
| -------------------------------------|
| [apple, kiwi, pineapple, mango, lime]|
| [strawberry, cherry, kiwi, orange] |
| [...] |
Desired Output:
| Fruits |
| -----------------------------------|
| apple, kiwi, pineapple, mango, lime|
| strawberry, cherry, kiwi, orange |
| ... |
I have tried this
for fruit in df['Fruits']:
df['Fruits'] = (','.join(word))
print(df['Fruits'])
The output is not what I want, and how can I append the new list to a new column (of course without the need of square brackets) anyone can help?
CodePudding user response:
IIUC, try this for string storage or list storge:
# String object
df_string = pd.DataFrame({'Fruits':['[apple, kiwi, pineapple, mango]',
'[strawberry, cherry, kiwi, orange]']})
df_string['Fruits'].str.replace('\[|\]','', regex=True)
Output:
0 apple, kiwi, pineapple, mango
1 strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object
Or,
#List object
df_list = pd.DataFrame({'Fruits':[['apple', 'kiwi', 'pineapple', 'mango'],
['strawberry', 'cherry', 'kiwi', 'orange']]})
df_list['Fruits'].agg(', '.join)
Output:
0 apple, kiwi, pineapple, mango
1 strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object
Note: df_list and df_string string representation looks identical.
print(df_list)
Fruits
0 [apple, kiwi, pineapple, mango]
1 [strawberry, cherry, kiwi, orange]
print(df_string)
Fruits
0 [apple, kiwi, pineapple, mango]
1 [strawberry, cherry, kiwi, orange]
CodePudding user response:
You can try to replace the []
characters by empty
characters with the replace()
function:
for fruit in df['Fruits']:
df['Fruits'] = df['Fruits'].replace("[", "").replace("]", "")
print(df['Fruits'])