Copy a column to multiple columns of a DataFrame with Pandas

I have a DataFrame with multiple columns, a few columns being NaN. The dataframe is quite big having around 5,000 columns. Below is a sample from it:

    GeoCode     ESP     FIN     USA     EZ19    PRT      
1   Geography   Spain   Finland USA     EZ      Portugal
2   31-Mar-15   NaN     NaN     0.26    0.89    NaN
3   30-Jun-15   NaN     NaN     NaN     0.90    NaN
4   30-Sep-15   NaN     NaN     0.31    0.90    NaN
5   31-Dec-15   NaN     NaN     0.41    0.91    NaN

I want to copy the value of column 'EZ19' to all columns where all values for row 2 and below are NaN. I tried the following code and it works:

nan_cols = df.columns[df_macro[2:].isnull().all()].to_list()
for c in nan_cols:
   df.loc[2:,c]= df.loc[2:,'EZ19']

But I was thinking there should be a way to assign value of column 'EZ19' to the target columns without using a loop and am surprised that there didn't seem to be a straight forward way to do this. Other questions here don't seem to handle the exact issue I have and couldn't find a solution that worked for me.

Given the size of my dataframe(and it is expected to grow larger overtime) I really want to avoid using a loop in my final code so any help with this will be greatly appreciated.



Read more here: https://stackoverflow.com/questions/66324740/copy-a-column-to-multiple-columns-of-a-dataframe-with-pandas

Content Attribution

This content was originally published by confused_certainties at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: