Python 删除熊猫中数据帧的前三行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16396903/
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-08-18 22:29:29  来源:igfitidea点击:

Delete the first three rows of a dataframe in pandas

pythonpandas

提问by Nilani Algiriyage

I need to delete the first three rows of a dataframe in pandas.

我需要删除熊猫中数据帧的前三行。

I know df.ix[:-1]would remove the last row, but I can't figure out how to remove first n rows.

我知道df.ix[:-1]会删除最后一行,但我不知道如何删除前 n 行。

采纳答案by bdiamante

Use iloc:

使用iloc

df = df.iloc[3:]

will give you a new df without the first three rows.

会给你一个没有前三行的新 df。

回答by beardc

You can use python slicing, but note it's not in-place.

您可以使用 python 切片,但请注意它不是就地的。

In [15]: import pandas as pd
In [16]: import numpy as np
In [17]: df = pd.DataFrame(np.random.random((5,2)))
In [18]: df
Out[18]:
          0         1
0  0.294077  0.229471
1  0.949007  0.790340
2  0.039961  0.720277
3  0.401468  0.803777
4  0.539951  0.763267

In [19]: df[3:]
Out[19]:
          0         1
3  0.401468  0.803777
4  0.539951  0.763267

回答by drexiya

I think a more explicit way of doing this is to use drop.

我认为这样做的更明确的方法是使用 drop。

The syntax is:

语法是:

df.drop(label)

And as pointed out by @tim and @ChaimG, this can be done in-place:

正如@tim 和@ChaimG 所指出的,这可以就地完成:

df.drop(label, inplace=True)

One way of implementing this could be:

实现这一点的一种方法可能是:

df.drop(df.index[:3], inplace=True)

And another "in place" use:

另一个“就地”使用:

df.drop(df.head(3).index, inplace=True)

回答by 176coding

df = df.iloc[n:]

n drops the first n rows.

n 删除前 n 行。

回答by Anupam khare

df.drop(df.index[[0,2]])

Pandas uses zero based numbering, so 0 is the first row, 1 is the second row and 2 is the third row.

Pandas 使用从零开始的编号,因此 0 是第一行,1 是第二行,2 是第三行。

回答by mxia

A simple way is to use tail(-n) to remove the first n rows

一个简单的方法是使用 tail(-n) 删除前 n 行

df=df.tail(-3)

df=df.tail(-3)

回答by Rahul kuchhadia

inp0= pd.read_csv("bank_marketing_updated_v1.csv",skiprows=2)

inp0= pd.read_csv("bank_marketing_updated_v1.csv",skiprows=2)

or if you want to do in existing dataframe

或者如果你想在现有的数据框中做

simply do following command

只需执行以下命令