【发布时间】:2020-10-13 14:22:52
【问题描述】:
我有一个 pandas 数据框,并且想要选择列的值以另一列的值开头的行。我尝试了以下方法:
import pandas as pd
df = pd.DataFrame({'A': ['apple', 'xyz', 'aa'],
'B': ['app', 'b', 'aa']})
df_subset = df[df['A'].str.startswith(df['B'])]
但它出错了,我发现 this solutions 也没有帮助。
KeyError: "None of [Float64Index([nan, nan, nan], dtype='float64')] are in the [columns]"
来自here 的np.where(df['A'].str.startswith(df['B']), True, False) 也为所有返回True。
【问题讨论】:
标签: python pandas string filter