Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas dataframe from column to row

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading