Home > database >  How to transpose data frame
How to transpose data frame

Time:03-30

I am working on a data frame which initially looks like :

         Date            Unnamed: 0  ...               Co             Ca
0           0                   NaN  ...              NaN            NaN
1  2022-01-21               Blablab  ...  33333333.333333  555555.555555
2  2022-01-21  BlaBlaBlaBlaBlaBlaBl  ...  44444444.444444  666666.666666

When I do :

MyData.iloc[-2]

It returns :

Name: 1, dtype: object
Date                2022-01-21
Unnamed: 0             Blablab
Unnamed: 1              WWWWWW
Unnamed: 2    123456789.123456
Co             33333333.333333
Ca               555555.555555

How would I get the below output :

1  2022-01-21               Blablab  ...  33333333.333333  555555.555555

I tried :

MyData.iloc[-2].T

But it is still in the same format ... What am I doing wrong ?

CodePudding user response:

iloc[i] returns a Series, which outputs as you showed. You want to output it in the way DataFrame is output. It so happens that .iloc[i:j] is a DataFrame. Just make it a one-row one:

MyData.iloc[-2:-2 1]

CodePudding user response:

This displays the row as you need it:

df.iloc[-2].to_frame().T

CodePudding user response:

The problem you're encountering here is that when you use iloc. to isolate a row, it's being returned as a series (not a dataframe).

Whatever it is you're wanting to do with this information, it may be best to leave it as a series.

  • Related