Tuesday, 27 September 2022

add dataframes columns names to rows after join procedure

I have the following dataframe:

df1 = pd.DataFrame({'ID'    : ['T1002.', 'T5006.', 'T5007.'],
                    'Parent': ['Stay home.', "Stay home.","Stay home."],
                    'Child' : ['2Severe weather.', "5847.", "Severe weather."]})



      ID    Parent       Child
0   T1002.  Stay home.  2Severe weather.
1   T5006.  Stay home.  5847.
2   T5007.  Stay home.  Severe weather.

I want to add the two columns into one and also add the columns' name into the rows. I want also the columns names to be in bold.

Expected outcome: (I cannot make bold the columns names ID, etc)

             Joined_columns()
0   ID: T1002.  Parent: Stay home.   Child: 2Severe weather.
1   ID: T5006.  Parent: Stay home.   Child: 5847.
2   ID: T5007.  Parent: Stay home.   Child: Severe weather.

The join is accomplished with the following code:

df1_final=df1.stack().groupby(level=0).apply(' '.join).to_frame(0)

But I am not sure how to go to the end. Any ideas



from add dataframes columns names to rows after join procedure

No comments:

Post a Comment