Skip to content
Advertisement

Using the items of a df as a header of a diffeerent dataframe

I have 2 dataframes

   df1=             0                      2
          1  _A1-Site_0_norm         _A1-Site_1_norm

and df2=

    0         2
2   0.500000  0.012903
3   0.010870  0.013793
4   0.011494  0.016260

I want to use df1 as a header of df2 so that df1 is either the header of the columns or the first raw.

    1  _A1-Site_0_norm  _A1-Site_1_norm
    2   0.500000          0.012903
    3   0.010870          0.013793
    4   0.011494          0.016260

i have multiple columns so it will not work to do

df2.columns=["_A1-Site_0_norm", "_A1-Site_1_norm"]

I thought of making a list of all the items present in the df1 to the use df2.columns and then include that list but I am having problems with converting the elements in row 1 of df1 of each column in items of a list.

I am not married to that approach any alternative to do it is wellcome Many thanks

Advertisement

Answer

if I understood you question correctly then this example should work for you

d={'A':[1],'B':[2],'C':[3]}
df = pd.DataFrame(data=d)
d2 = {'1':['D'],'2':['E'],'3':['F']}
df2 = pd.DataFrame(data=d2)
df.columns = df2.values.tolist() #this is what you need to implement
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement