[Solved] How to insert list of list items in already created dataframe in pandas? [closed]


You can use np.ravel:

df["new"] = np.array(list_item).ravel()

print (df)

     Name Code City new
0    Shiv   Sh  ALD   1
1   Kumar   KR   PJ   2
2     Ram   RM   KL   3
3   Shank   SK   RM   5
4    Jeet   JT  PKG   6
5    Atul   AT  FTP   7
6  Ganesh   GS   TL   9
7  Kishor   KH   KI  10
8   Gagan   GN   AK  11

After Updated Question:

list_item = [['1|x', '2|x', '3|-'],
             ['5|x', '6|-', '7|x'],
             ['9|x', '10|x', '11|-']]
s = pd.Series(np.array(list_item).ravel())
df[['Num', 'Expr']] = s.str.split('|', n=1, expand=True)

df
     Name Code City Num Expr
0    Shiv   Sh  ALD   1    x
1   Kumar   KR   PJ   2    x
2     Ram   RM   KL   3    -
3   Shank   SK   RM   5    x
4    Jeet   JT  PKG   6    -
5    Atul   AT  FTP   7    x
6  Ganesh   GS   TL   9    x
7  Kishor   KH   KI  10    x
8   Gagan   GN   AK  11    -

6

solved How to insert list of list items in already created dataframe in pandas? [closed]