1

I have a pandas dataframe like this below:

A B C
a b c
d e f

where A B and C are column names. Now i have a list:

mylist = [1,2,3]

I want to replace the c in column C with list such as dataframe expands for all value of list, like below:

A B C
a b 1
a b 2
a b 3
d e f

Any help would be appreciated!

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Shubzumt
  • 143
  • 1
  • 12
  • I can always do this by running it in for loop, but need some efficient solution for large datasets – Shubzumt May 23 '18 at 07:17
  • Do `df.loc[df.index[0], 'C'] = mylist` and then follow instructions at https://stackoverflow.com/questions/49147996/explode-column-of-list-to-multiple-rows/49148066#49148066 – cs95 May 23 '18 at 07:18

2 Answers2

2

I tried this,

mylist = [1,2,3]
x=pd.DataFrame({'mylist':mylist})
x['C']='c'
res= pd.merge(df,x,on=['C'],how='left')
res['mylist']=res['mylist'].fillna(res['C'])

For further,

del res['C']
res.rename(columns={"mylist":"C"},inplace=True)
print res

Output:

   A  B  C
0  a  b  1
1  a  b  2
2  a  b  3
3  d  e  f
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
  • the inner join method is good here, but in large datasets it takes a lot of time, thats why I am looking for something efficient. – Shubzumt May 23 '18 at 10:16
1

You can use:

print (df)
   A  B  C
0  a  b  c
1  d  e  f
2  a  b  c
3  t  e  w
mylist = [1,2,3]

idx1 = df.index[df.C == 'c']
df = df.loc[idx1.repeat(len(mylist))].assign(C=mylist * len(idx1)).append(df[df.C != 'c'])
print (df)
   A  B  C
0  a  b  1
0  a  b  2
0  a  b  3
2  a  b  1
2  a  b  2
2  a  b  3
1  d  e  f
3  t  e  w
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252