pandas 如何使用熊猫删除第一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44987224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:57:11  来源:igfitidea点击:

How to drop first row using pandas?

pythonpandas

提问by Robert Padgett

I've searched at other questions related to dropping rows but could not find one that worked:

我搜索了与删除行相关的其他问题,但找不到有效的问题:

I have a CSV file exported from the tool screaming frog that looks like this:

我有一个从尖叫青蛙工具导出的 CSV 文件,如下所示:

Internal - HTML |               |             |
--------------- | --------------|-------------|
   Address      |   Content     | Status Code |
----------------|---------------|-------------|
www.example.com |   text/html   |   200       |

I want to remove the first row that contains 'Internal - HTML'. When analyzing it with df.keys()I get this information" Index(['Internal - HTML'], dtype='object').

我想删除包含“内部 - HTML”的第一行。当df.keys()我用它分析它时,我得到了这些信息” Index(['Internal - HTML'], dtype='object')

I want to use the second row as the Index, which contains the correct column labels.

我想使用第二行作为索引,其中包含正确的列标签。

When I use the code:

当我使用代码时:

a = pandas.read_csv("internal_html.csv", encoding="utf-8")
a.drop('Internal - HTML')
a.head(3)

I get this error: KeyError: 'Internal - HTML'

我收到此错误: KeyError: 'Internal - HTML'

I also tried what was suggested here Remove index name in pandasand also tried resetting the index:

我还尝试了此处建议的删除Pandas中的索引名称并尝试重置索引:

a = pandas.read_csv("internal_html.csv", encoding="utf-8")
a.reset_index(level=0, drop=True)
a.head(3)

None of the options above worked.

上述选项均无效。

回答by PRMoureu

You can add headeras a parameter in the first call, to use column names and start of data :

您可以header在第一次调用时作为参数添加,以使用列名和数据开头:

a = pandas.read_csv("internal_html.csv", encoding="utf-8", header=1)

回答by student

Not exactly sure about how data is in csv, but I think you can use skiprows=1while reading the csv:

不完全确定数据是如何输入的csv,但我认为您可以skiprows=1在阅读以下内容时使用csv

a = pd.read_csv("internal_html.csv", encoding="utf-8")
a.keys()

Output:

输出:

Index(['Internal - HTML'], dtype='object')

Looking at df(Assuming data is in following format):

查看df假设数据格式如下):

print(a)

Output:

输出:

                            Internal - HTML
Address            Content   Status Code   
www.example.com   text/html     200        

Now, using skiprowsto read the .csvfile:

现在,使用skiprows读取.csv文件:

a = pd.read_csv("internal_html.csv", encoding="utf-8", skiprows=1)
print(a.keys())

Output:

输出:

Index(['Address', '   Content', 'Status Code'], dtype='object')

Observing dataframe a:

观察数据框a

print(a)

Output:

输出:

           Address      Content       Status Code
  0  www.example.com    text/html     200