Home > Software design >  Pandas dataframe from column to row
Pandas dataframe from column to row

Time:10-06

I want to extract data from excel:

PN = df.iat[6,8]
LF = df.iloc[79:151,8].to_list()

d1 =  {"PN":PN,"LF":LF}
d3=pd.DataFrame(d1)
print(d3)

This is what it prints:

                      PN        LF
0   105222331-04  1DJ    -0.002429
1   105222331-04  1DJ     0.002642
2   105222331-04  1DJ     0.006156
3   105222331-04  1DJ     0.009979
4   105222331-04  1DJ     0.010492

I want to make it look like this:

 PN                  LF          LF         LF
105222331-04  1DJ    -0.002429   0.002642   0.006156

CodePudding user response:

You can create one row DataFrame by join lists:

L = df.iloc[6, [8]].tolist()   df.iloc[79:151,8].tolist()

d3 = pd.DataFrame([L])

Duplicated columns names are not recommended, because problem with select, but possible:

L = df.iloc[6, [8]].tolist()   df.iloc[79:151,8].tolist()
cols = ['PN']   ['LF'] * (len(L) - 1)
d3 = pd.DataFrame([L], columns=cols)
print (d3)
  • Related