简体   繁体   English

如果特定列中的值不是 Pandas 数据框中的整数,则删除行

[英]Drop rows if value in a specific column is not an integer in pandas dataframe

If I have a dataframe and want to drop any rows where the value in one column is not an integer how would I do this?如果我有一个数据框并想删除其中一列中的值不是整数的任何行,我该怎么做?

The alternative is to drop rows if value is not within a range 0-2 but since I am not sure how to do either of them I was hoping someonelse might.如果值不在 0-2 范围内,另一种方法是删除行,但由于我不确定如何执行其中任何一个,我希望有人可以这样做。

Here is what I tried but it didn't work not sure why:这是我尝试过的,但不知道为什么:

df = df[(df['entrytype'] != 0) | (df['entrytype'] !=1) | (df['entrytype'] != 2)].all(1)

There are 2 approaches I propose:我提出了两种方法:

In [212]:

df = pd.DataFrame({'entrytype':[0,1,np.NaN, 'asdas',2]})
df
Out[212]:
  entrytype
0         0
1         1
2       NaN
3     asdas
4         2

If the range of values is as restricted as you say then using isin will be the fastest method:如果值的范围如您所说的那样受限制,那么使用isin将是最快的方法:

In [216]:

df[df['entrytype'].isin([0,1,2])]
Out[216]:
  entrytype
0         0
1         1
4         2

Otherwise we could cast to a str and then call .isdigit()否则我们可以转换为 str 然后调用.isdigit()

In [215]:

df[df['entrytype'].apply(lambda x: str(x).isdigit())]
Out[215]:
  entrytype
0         0
1         1
4         2

str("-1").isdigit() is False str("-1").isdigit()False

str("-1").lstrip("-").isdigit() works but is not nice. str("-1").lstrip("-").isdigit()工作但不好。


df.loc[df['Feature'].str.match('^[+-]?\\d+$')]

for your question the reverse set对于您的问题,反向设置

df.loc[ ~(df['Feature'].str.match('^[+-]?\\d+$')) ]

We have multiple ways to do the same, but I found this method easy and efficient.我们有多种方法可以做到这一点,但我发现这种方法既简单又高效。

Quick Examples快速示例

#Using drop() to delete rows based on column value
df.drop(df[df['Fee'] >= 24000].index, inplace = True)

# Remove rows
df2 = df[df.Fee >= 24000]

# If you have space in column name
# Specify column name with in single quotes
df2 = df[df['column name']]

# Using loc
df2 = df.loc[df["Fee"] >= 24000 ]

# Delect rows based on multiple column value
df2 = df[ (df['Fee'] >= 22000) & (df['Discount'] == 2300)]

# Drop rows with None/NaN
df2 = df[df.Discount.notnull()]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除带有条件的 Pandas DataFrame 行以保留特定列值 - How to drop Pandas DataFrame rows with condition to keep specific column value 如何在Pandas DataFrame的特定列中达到最小值后删除行? - How to drop rows after minimum value reached in specific column in Pandas DataFrame? 在pandas数据框的任何列中删除带有“问号”值的行 - Drop rows with a 'question mark' value in any column in a pandas dataframe 如何删除Pandas DataFrame某列值为NaN的行 - How to drop rows of Pandas DataFrame whose value in a certain column is NaN Pandas 在一个 dataframe 中删除与另一个 dataframe 的列中的行共享一个共同值的行 - Pandas drop rows in one dataframe that share a common value with a rows in a column of another dataframe 熊猫根据特定的列值将数据框中的行分组 - Pandas group the rows in a dataframe based on specific column value 熊猫DataFrame列中特定值的连续行的累积计数 - Cumulative count for consecutive rows of a specific value in a pandas DataFrame column 将pandas数据框基于整数列拆分为行 - Split a pandas dataframe into rows based on an integer column 按列值删除 Pandas DataFrame 中的行(文本) - Drop rows in Pandas DataFrame by Column values (text) 如何在熊猫数据框中的列中删除带有“nan”的行? - how to drop rows with 'nan' in a column in a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM