gpt4 book ai didi

python - Pandas notnull 不适用于数据框中的列

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:49 25 4
gpt4 key购买 nike

后续问题 Combine Pandas data frame column values into new column

我已成功将一系列 ID 组合到一个字段中,现在我需要过滤掉任何未以组合 ID 值结尾的行。通常我会使用 notnull 但在这个列上它不起作用。有人可以帮我解决这个问题吗?谢谢!

df_merged['Combined_ID']  = df_merged[['ID1','ID2','ID3','ID4','ID5']].apply(lambda x : ''.join([e for e in x if isinstance(e, basestring)]), axis=1)

#Remove any rows that do not have an ID in the new field
#This is not removing the rows that do not have a combined ID value
df_merged = df_merged[pd.notnull(df_merged['Combined_ID'])]

最佳答案

该列永远不会为空。如果行中的每个项目都不是基字符串,则该函数返回 ''

因此以下应该有效:

df_merged = df_merged[df_merged['Combined_ID'] != '']

关于python - Pandas notnull 不适用于数据框中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com