Afke
Afke

Reputation: 989

Pandas Dataframe dropping rows and setting column names

I have a pandas dataframe like this:

      1     2       3 
0
NaN   ingr   contr   count
2.0   water  0.02    275
3.0   oil    0.23    11
.....

and I would like to transfer it to this:

ingr   contr   count
water  0.02    275
oil    0.23    11
....

I tried to use pd.drop and pd.set_index() etc but I cannot figure out how to do this. Anyone can help me?

Upvotes: 0

Views: 89

Answers (2)

jezrael
jezrael

Reputation: 863741

Use iloc with reset_index and rename_axis (new in pandas 0.18.0):

df.columns = df.iloc[0,:]
print (df.iloc[1:,:].reset_index().rename_axis(None, axis=1))
     0   ingr contr count
0  2.0  water  0.02   275
1  3.0    oil  0.23    11

If dont need old index add parameter drop=True:

df.columns = df.iloc[0,:]
print (df.iloc[1:,:].reset_index(drop=True).rename_axis(None, axis=1))
    ingr contr count
0  water  0.02   275
1    oil  0.23    11

If you need first column as index use set_index:

df.columns = df.iloc[0,:]
print (df.iloc[1:,:].set_index('ingr').rename_axis(None, axis=1).rename_axis(None))
      contr count
water  0.02   275
oil    0.23    11

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

try iloc indexer:

In [66]: df
Out[66]:
          1      2      3
NaN    ingr  contr  count
 2.0  water   0.02    275
 3.0    oil   0.23     11

In [67]: df.columns = df.iloc[0]

In [68]: df = df.iloc[1:].reset_index()

In [69]: df
Out[69]:
nan  index   ingr contr count
0      2.0  water  0.02   275
1      3.0    oil  0.23    11

PS but it would be much more efficient to read your data properly from the very beginning using header, skiprows, etc. parameters of the read_csv()/read_excel/read_table/et.c functions

Upvotes: 1

Related Questions