panda是删除行_如何从Pandas数据帧中删除行列表?

如何从Pandas数据帧中删除行列表?

我有一个数据帧df:

df

sales discount net_sales cogs

STK_ID RPT_Date

600141 20060331 2.709 NaN 2.709 2.245

20060630 6.590 NaN 6.590 5.291

20060930 10.103 NaN 10.103 7.981

20061231 15.915 NaN 15.915 12.686

20070331 3.196 NaN 3.196 2.710

20070630 7.907 NaN 7.907 6.459

然后我想删除列表中显示的某些序列号的行,假设这里是[1,2,4],然后离开:

sales discount net_sales cogs

STK_ID RPT_Date

600141 20060331 2.709 NaN 2.709 2.245

20061231 15.915 NaN 15.915 12.686

20070630 7.907 NaN 7.907 6.459

如何或有什么功能可以做到这一点?

bigbug asked 2019-03-14T14:06:37Z

7个解决方案

299 votes

使用DataFrame.drop并传递一系列索引标签:

In [65]: df

Out[65]:

one two

one 1 4

two 2 3

three 3 2

four 4 1

In [66]: df.drop(df.index[[1,3]])

Out[66]:

one two

one 1 4

three 3 2

Theodros Zelleke answered 2019-03-14T14:07:05Z

83 votes

请注意,当您想要执行下拉行时,使用”inplace”命令可能很重要。

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

因为您的原始问题没有返回任何内容,所以应该使用此命令。[http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.drop.html]

user3155053 answered 2019-03-14T14:07:48Z

38 votes

您还可以传递给DataFrame.drop标签本身(而不是系列索引标签):

In[17]: df

Out[17]:

a b c d e

one 0.456558 -2.536432 0.216279 -1.305855 -0.121635

two -1.015127 -0.445133 1.867681 2.179392 0.518801

In[18]: df.drop(‘one’)

Out[18]:

a b c d e

two -1.015127 -0.445133 1.867681 2.179392 0.518801

这相当于:

In[19]: df.drop(df.index[[0]])

Out[19]:

a b c d e

two -1.015127 -0.445133 1.867681 2.179392 0.518801

danielhadar answered 2019-03-14T14:08:36Z

28 votes

如果DataFrame很大,并且要删除的行数也很大,那么索引20.5s的简单删除会花费太多时间。

在我的情况下,我有一个带有20.5s的浮点数的多索引DataFrame,我需要从中删除df.drop行。 我找到的最快的方法是非常违反直觉的5min 27s。

设20.5s是要删除的位置索引数组(问题中为df.drop)。

indexes_to_keep = set(range(df.shape[0])) – set(indexes_to_drop)

df_sliced = df.take(list(indexes_to_keep))

在我的情况下,这花了20.5s,而简单的df.drop花了5min 27s并消耗了大量的内存。 生成的DataFrame是相同的。

Dennis Golomazov answered 2019-03-14T14:09:47Z

7 votes

如果我想删除一个让我们说索引为unwanted_indices的行,我会执行以下操作:

df = df[df.index != x]

如果我想删除多个索引(比如这些索引在列表unwanted_indices中),我会这样做:

desired_indices = [i for i in len(df.index) if i not in unwanted_indices]

desired_df = df.iloc[desired_indices]

Divyansh answered 2019-03-14T14:10:38Z

7 votes

我以一种更简单的方式解决了这个问题 – 只需两步。

步骤1:首先形成包含不需要的行/数据的数据帧。

步骤2:使用此不需要的数据帧的索引从原始数据帧中删除行。

假设您有一个数据帧df,其中包含’Age’这一整数列。 现在让我们假设您要删除所有行,其中’Age’为负数。

步骤1:df_age_negative = df [df [‘Age’]< 0]

第2步:df = df.drop(df_age_negative.index,axis = 0)

希望这更简单,并帮助您。

Krishnaprasad Challuru answered 2019-03-14T14:12:25Z

3 votes

在对@theodros-zelleke的回答的评论中,@ j-jones询问如果索引不是唯一的,该怎么做。 我不得不处理这种情况。 我做的是在我拨打rename_duplicates()之前重命名索引中的重复项,la:

dropped_indexes =

df.index = rename_duplicates(df.index)

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

其中rename_duplicates()是我定义的函数,它通过索引元素并重命名了重复项。 我使用与列上的pd.read_csv()相同的重命名模式,即”%s.%d” % (name, count),其中name是行的名称,count是先前发生的次数。

mepstein answered 2019-03-14T14:13:15Z

Original: https://blog.csdn.net/weixin_32958777/article/details/113020544
Author: 一把儿韭菜
Title: panda是删除行_如何从Pandas数据帧中删除行列表?

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/755589/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球