简体   繁体   English

有条件地从熊猫数据框中删除行

[英]remove rows from pandas data frame with condition

I have a pandas data frame...in one of its columns, there is list of strings. 我有一个熊猫数据框...在其中一列中,有字符串列表。 I want to define a condition for it. 我想为此定义一个条件。 This condition is that if a list in each row has the length less than 2 strings, drop whole row from data frame and make a new one. 这种情况是,如果每行中的列表的长度小于2个字符串,则从数据帧中删除整行并创建一个新行。 I use to write this code for it . 我经常为此编写代码。 but it doesn't work! 但这不起作用!

new_dataframe = dataframe.drop(x for x in dataframe['specific column'][:] if x in len(dataframe['specific column'][:])<2)

[:] is there to consider all rows of this specific column [:]可以考虑此特定列的所有行

and I receive this error: 我收到此错误:

'labels [<generator object <genexpr> at 0x7fcc19dd80a0>] not contained in axis'

Try: 尝试:

# Test dataframe with lists of strings
df = pd.DataFrame({"specific column": [
    ["a", "b"],
    ["a", "b", "c",],
    ["a",],
    ["a", "b", "c", "d"]], })

# Select indices of rows with less than 2 items in list
ix = df["specific column"].str.len() < 2

# Select all other rows
df.loc[~ix]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM