Home > Mobile >  How can I map a list of values to a dataframe
How can I map a list of values to a dataframe

Time:05-17

i'm trying to map some values to a dataframe, i used some looping methods but it seems that there must be a simple way to acheive the result im looking for

input_df :

A B C
x y z
i j t
f g h

list if values to map :

list = [1,2,3]

result_df :

A B C D
x y z 1
i j t 1
f g h 1
x y z 2
i j t 2
f g h 2
x y z 3
i j t 3
f g h 3

CodePudding user response:

Try a cross join (i.e. Cartesian product):

tmp = pd.DataFrame(list, columns=["D"])
tmp.merge(input_df, how="cross")

Require pandas >= 1.2.0. pd.merge

  • Related