Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over list elements in pandas dataframe - each entry has different size and a new column needs to be generated w.r.t each entry in list

I have a dataframe

enter image description here

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.

like image 261
nOObda Avatar asked Mar 07 '23 20:03

nOObda


2 Answers

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
like image 173
BENY Avatar answered Apr 08 '23 10:04

BENY


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
like image 31
jpp Avatar answered Apr 08 '23 12:04

jpp