I have dataframe where a column named teams which the dtype is "O"(str) but inside it there is list: eg: "['Australia', 'Sri Lanka']" Now i want to split this two team names into two columns, how to do it?
Let's take this example with both types of object columns.
df = pd.DataFrame({'list_col':[['Aaaaa','Bbbbb'],['Scott','Boston']],
'str_col':["[Ccccc,Ddddd]","[Scott,Boston]"]})
print(df)
Output:
list_col str_col
0 [Aaaaa, Bbbbb] [Ccccc,Ddddd]
1 [Scott, Boston] [Scott,Boston]
With df.info():
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 list_col 2 non-null object
1 str_col 2 non-null object
dtypes: object(2)
memory usage: 160.0+ bytes
The both look the same, now:
type(df.iloc[0,0])
returns list
type(df.iloc[0,1])
returns str
Let's separate like this:
df['str_col'].str.strip('\[|\]').str.split(',',expand=True)
0 1
0 Ccccc Ddddd
1 Scott Boston
Or like this:
dfe = df[['list_col']].explode('list_col')
dfe.set_index(dfe.groupby(level=0).cumcount(), append=True)['list_col'].unstack()
0 1
0 Aaaaa Bbbbb
1 Scott Boston
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.