Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas automatically converts row to column

I have a very simple dataframe like so:

In [8]: df
Out[8]: 
   A  B  C
0  2  a  a
1  3  s  3
2  4  c  !
3  1  f  1

My goal is to extract the first row in such a way that looks like this:

   A  B  C
0  2  a  a

As you can see the dataframe shape (1x3) is preserved and the first row still has 3 columns.

However when I type the following command df.loc[0] the output result is this:

df.loc[0]
Out[9]: 
A    2
B    a
C    a
Name: 0, dtype: object

As you can see the row has turned into a column with 3 rows! (3x1 instead of 3x1). How is this possible? how can I simply extract the row and preserve its shape as described in my goal? Could you provide a smart and elegant way to do it?

I tried to use the transpose command .T but without success... I know I could create another dataframe where the columns are extracted by the original dataframe but this way quite tedious and not elegant I would say (pd.DataFrame({'A':[2], 'B':'a', 'C':'a'})).

Here is the dataframe if you need it:

import pandas as pd
df = pd.DataFrame({'A':[2,3,4,1], 'B':['a','s','c','f'], 'C':['a', 3, '!', 1]})
like image 778
Federico Gentile Avatar asked Jun 01 '17 07:06

Federico Gentile


People also ask

How do I convert rows to columns in pandas?

The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied.

How do I turn first row into column names in pandas?

You can use df. columns=df. iloc[0] to set the column labels by extracting the first row. In pandas, the index starts from 0 hence 0 means first row.

How do I turn a row into a series in pandas?

Convert DataFrame Row to SeriesUse squeeze() function to convert the single Pandas DataFrame row to series. For instance, df. iloc[2]. reset_index(drop=True).


2 Answers

You need add [] for DataFrame:

#select by index value
print (df.loc[[0]])
   A  B  C
0  2  a  a

Or:

print (df.iloc[[0]])
   A  B  C
0  2  a  a

If need transpose Series, first need convert it to DataFrame by to_frame:

print (df.loc[0].to_frame())
   0
A  2
B  a
C  a

print (df.loc[0].to_frame().T)
   A  B  C
0  2  a  a
like image 140
jezrael Avatar answered Oct 05 '22 23:10

jezrael


Use a range selector will preserve the Dataframe format.

df.iloc[0:1]
Out[221]: 
   A  B  C
0  2  a  a
like image 27
Allen Avatar answered Oct 05 '22 22:10

Allen