I have a dataframe
Here i have a column called "name split" which is a column with lists. Now i want to split the contents of the lists and create separate columns for each.
This is what i have tried so far :
df = pd.read_csv("C:/Users/Transorg-PC/Desktop/Training/py/datase/football.csv")
temp = df.copy()
temp['name'] = temp['name'].apply(lambda x: ' '.join(x.split()))
temp['name split'] = temp['name'].apply(lambda x: x.split())
temp['length'] = temp['name split'].str.len()
for i in range(temp['length'].max()-1):
temp[i] = temp['name split'].apply(lambda x:x[i])
But i am not able to iterate like this as for some cases the index goes out of bound. So how to split the contents of the list in separate columns.
Something Like Data from jpp
pd.concat([df,pd.DataFrame(df.name.tolist())],1)
Out[1596]:
A name 0 1
0 1 [1, 2] 1 2
1 1 [3, 4] 3 4
2 2 [5, 6] 5 6
Update
df=pd.DataFrame([[1,[1, 2]],
[1,[3, 4]],
[2,[5, 6,1,1]]],
columns=['A','name'])
pd.concat([df,pd.DataFrame(df.name.tolist())],1)
Out[1602]:
A name 0 1 2 3
0 1 [1, 2] 1 2 NaN NaN
1 1 [3, 4] 3 4 NaN NaN
2 2 [5, 6, 1, 1] 5 6 1.0 1.0
This is one way:
df = pd.DataFrame([[1,[1, 2, 3]],
[1,[3, 4]],
[2,[5, 6, 7, 8]]],
columns=['A','name'])
df = df.join(pd.DataFrame(df['name'].tolist()))
# A name 0 1 2 3
# 0 1 [1, 2, 3] 1.0 2.0 3.0 NaN
# 1 1 [3, 4] 3.0 4.0 NaN NaN
# 2 2 [5, 6, 7, 8] 5.0 6.0 7.0 8.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With