Home > Net >  how do I add list values at a specific column in pandas?
how do I add list values at a specific column in pandas?

Time:01-05

Lets say I have the following dataframe:

df = pd.DataFrame({'sample': ['sample 1', 'sample 2'],
                   'values': [[0.2, 0.3, 0.5],[0.3, 0.3, 0.4]],
                   'group': [1, 0]})

output:

    sample         values      group
0   sample 1    [0.2, 0.3, 0.5] 1
1   sample 2    [0.3, 0.3, 0.4] 0

and a list of group

 lst=[0, 1, 2]

I would like to insert all values of lst for all rows in the group column in df

Expected output:

enter image description here

CodePudding user response:

You can try this:

import numpy as np

new_df = pd.DataFrame(np.repeat(df.values, len(lst), axis=0), columns=df.columns)
new_lst = lst * len(df)
new_df['group'] = new_lst
print(new_df)

Output:

     sample           values  group
0  sample 1  [0.2, 0.3, 0.5]      0
1  sample 1  [0.2, 0.3, 0.5]      1
2  sample 1  [0.2, 0.3, 0.5]      2
3  sample 2  [0.3, 0.3, 0.4]      0
4  sample 2  [0.3, 0.3, 0.4]      1
5  sample 2  [0.3, 0.3, 0.4]      2

CodePudding user response:

Use a cross merge:

out = df.drop(columns='group').merge(pd.Series(lst, name='group'), how='cross')

Output:

     sample           values  group
0  sample 1  [0.2, 0.3, 0.5]      0
1  sample 1  [0.2, 0.3, 0.5]      1
2  sample 1  [0.2, 0.3, 0.5]      2
3  sample 2  [0.3, 0.3, 0.4]      0
4  sample 2  [0.3, 0.3, 0.4]      1
5  sample 2  [0.3, 0.3, 0.4]      2
  • Related