需要帮助来解决Unnamed并在 pandas 的dataframe中进行更改 [英] Need help to solve the Unnamed and to change it in dataframe in pandas
本文介绍了需要帮助来解决Unnamed并在 pandas 的dataframe中进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在python中将索引从未命名设置为数据框的第一行
how set my indexes from "Unnamed" to the first line of my dataframe in python
import pandas as pd
df = pd.read_excel('example.xls','Day_Report',index_col=None ,skip_footer=31 ,index=False)
df = df.dropna(how='all',axis=1)
df = df.dropna(how='all')
df = df.drop(2)
推荐答案
要将列名(假设这就是索引的意思)设置为第一行,可以使用
To set the column names (assuming that's what you mean by "indexes") to the first row, you can use
df.columns = df.loc[0, :].values
接着,如果要删除第一行,可以使用
Following that, if you want to drop the first row, you can use
df.drop(0, inplace=True)
编辑
正如Coldspeed在下面正确指出的那样,如果该源正在读取CSV,则添加 skiprows = 1
参数要好得多。
As coldspeed correctly notes below, if the source of this is reading a CSV, then adding the skiprows=1
parameter is much better.
这篇关于需要帮助来解决Unnamed并在 pandas 的dataframe中进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文