Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python dataframe: return column name in apply lambda

I want to use apply(applymap) and lambda functions to get the column name for every element in dfx (a dataframe). The lambda function is used for mapping another dataframe into dfx, and it will be rewritten afterwards.

dfx

             A     B    C
2011-01-10   123   12   123 
2011-01-10   12    32   312
2011-01-11   44    1    30.99   

pseudocode

output = dfx.apply(lambda r:r.column)

I don't know how to write this part "r:r.column"

intended output

             A    B    C
2011-01-10   A    B    C 
2011-01-10   A    B    C
2011-01-11   A    B    C  

Any help is more than welcome! Thanks a lot!!

like image 926
Yang Avatar asked May 16 '26 23:05

Yang


1 Answers

You can assign via pd.DataFrame.iloc:

df.iloc[:] = df.columns

print(df)

            A  B  C
2011-01-10  A  B  C
2011-01-10  A  B  C
2011-01-11  A  B  C
like image 122
jpp Avatar answered May 18 '26 13:05

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!