Search code examples
pythonpandasdataframeassign

How do I iterate over list for keyword in pd.DataFrame.assign()?


I can't get passed a problem in Pandas. I'm quite new to python, so, this could be dump question but I thought I ask it anyway. :)

I'm trying to replace a number of columns in a Dataframe by looping over the labels and replace the data. Since, I want to create a new Dataframe I figured assign() is the best option. The problem is that I want to iterate over the labels and use the the resulting string as keyword for the assign function.

labels_list = ["Label1","Label2","Label3"]

for label in labels_list:
    df3 = df1.assign(label=df2[label])

What is does, it just appends the column from df2 with the label from the loop at the end of df3. If I insert the string fromt the list directly like this:

df3 = df1.assign(label1=df2[label1])

It does the job correctly. Does any know why I behaves like that and how to solve this problem?

Thx


Solution

  • Use unpacking:

    df1 = pd.DataFrame()
    df2 = pd.DataFrame(np.ones((5, 3)), columns=labels_list)
    df3 = df1.assign(**{k:df2[k] for k in labels_list})
    print(df3)
    

    Output:

       Label1  Label2  Label3
    0     1.0     1.0     1.0
    1     1.0     1.0     1.0
    2     1.0     1.0     1.0
    3     1.0     1.0     1.0
    4     1.0     1.0     1.0