[英]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. 我试图使用
pd.drop
和pd.set_index()
等,但是我不知道该怎么做。 Anyone can help me? 有人可以帮助我吗?
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 PS,但它会更有效地从一开始就使用正确读取你的数据
header
, skiprows
等参数read_csv() /read_excel/read_table/et.c功能
Use iloc
with reset_index
and rename_axis
(new in pandas
0.18.0
): 将
iloc
与reset_index
和rename_axis
( 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
: 如果不需要旧索引,则添加参数
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
: 如果您需要第一列作为索引,请使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.