后续问题 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'] != '']
我是一名优秀的程序员,十分优秀!