'How to transpose data frame
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 ?
Solution 1:[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]
Solution 2:[2]
This displays the row as you need it:
df.iloc[-2].to_frame().T
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amadan |
| Solution 2 | vtasca |
| Solution 3 |
