I have a dataframe like this:
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': ['aa', 'b', 'c']})
A B
0 1 aa
1 2 b
2 3 c
I want to convert each row of column B to a list. For example, my desired output is something like this:
df_new
A B
0 1 [aa]
1 2 [b]
2 3 [c]
>Solution :
You can use split to do stuff.
import pandas as pd
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': ['a', 'b', 'c']})
df['B'] = df['B'].apply(lambda x: x.split(','))
print(df)