0

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 ?

TourEiffel
  • 4,034
  • 2
  • 16
  • 45
  • Somehow I don't understand your question - do you want to have all the records in one row? – teoML Mar 29 '22 at 08:47

3 Answers3

1

This displays the row as you need it:

df.iloc[-2].to_frame().T
vtasca
  • 1,660
  • 11
  • 17
1

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]
Amadan
  • 191,408
  • 23
  • 240
  • 301
1

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.

JED HK
  • 216
  • 1
  • 8