-4

I need to fill list of list items into already defined dataframe row by row. Can you please help me how to do this?

I am really sorry I am changing my requirement.

Example:

list_item = [['1|x', '2|x', '3|-'],
             ['5|x', '6|-', '7|x'],
             ['9|x', '10|x', '11|-']]

Pre defined dataframe: df

Name     Code     City  
Shiv     Sh       ALD
Kumar    KR       PJ
Ram      RM       KL
Shank    SK       RM
Jeet     JT       PKG
Atul     AT       FTP
Ganesh   GS       TL
Kishor   KH       KI
Gagan    GN       AK 

Final Output:

df:

Name     Code     City  Num  Expr
Shiv     Sh       ALD   1     x
Kumar    KR       PJ    2     x
Ram      RM       KL    3     -
Shank    SK       RM    5     x
Jeet     JT       PKG   6     -
Atul     AT       FTP   7     x
Ganesh   GS       TL    9     x
Kishor   KH       KI    10    x 
Gagan    GN       AK    11    x
shivam
  • 75
  • 1
  • 3
  • 11
  • Possible duplicate of [How to split a column into two columns?](https://stackoverflow.com/questions/14745022/how-to-split-a-column-into-two-columns) with a trivial addition of flattening an array before hand. – cs95 Oct 29 '20 at 05:41

2 Answers2

1

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    -
cs95
  • 379,657
  • 97
  • 704
  • 746
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Thank you for your response. I am really sorry I changed my requirement. Could you please check and help me? – shivam Oct 29 '20 at 05:15
  • 3
    Please avoid changing requirements afterwards. This invalidates the effort people took to answer your question. – Henry Yik Oct 29 '20 at 05:19
  • @shivam out of respect for others' time and effort, I would accept a solution and open a new question linking back to this for reference. You will also avoid an onslaught of negative votes. – David Erickson Oct 29 '20 at 05:25
  • @shivam I updated Henry's answer. Henry, I hope you don't mind. – David Erickson Oct 29 '20 at 05:33
  • 1
    Its fine, but better is to assign both columns in one go using expand=True. – Henry Yik Oct 29 '20 at 05:40
  • Good point, cs95 beat me to it. I was going to do `df[["Num", "Expr"]] = pd.Series(np.array(list_item).ravel()).str.split('|', expand=True)` – David Erickson Oct 29 '20 at 05:45
0

Not as well known, but pandas actually has a built-in flatten function for flattening lists (or any other container-like) of any level of nestedness. flatten returns a generator, so we need to wrap it in a pd.Series() (wrapping it in list() works as well- anything that will exhaust the gernerator) to retrieve all of the values.

import pandas as pd
from pandas.core.common import flatten

df["new"] = pd.Series(flatten(list_item))

Edit: Since you've changed your question. You can achieve the same result simply with the flatten approach: We're flattening the list, and for each item we're splitting the string on the "|" to create a new list of lists (organized properly this time). We can assign this directly to new columns.

df[["Num", "Expr"]] = [item.split("|") for item in flatten(list_item)]
Cameron Riddell
  • 10,942
  • 9
  • 19
  • Thank you for your response. I am sorry I changed my requirement. Could you please check and help me to get this result. – shivam Oct 29 '20 at 05:16