我有一个熊猫数据框...在其中一列中,有字符串列表。我想为此定义一个条件。这种情况是,如果每行中的列表的长度小于2个字符串,则从数据帧中删除整行并创建一个新行。 我经常为此编写代码。但这不起作用!
new_dataframe = dataframe.drop(x for x in dataframe['specific column'][:] if x in len(dataframe['specific column'][:])<2)
[:]可以考虑此特定列的所有行
我收到此错误:
'labels [<generator object <genexpr> at 0x7fcc19dd80a0>] not contained in axis'
答案 0 :(得分:2)
尝试:
# 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]