[英]Select rows where value of column A starts with value of column B
我有一個 pandas dataframe 並且想要 select 行,其中列的值以另一列的值開頭。 我嘗試了以下方法:
import pandas as pd
df = pd.DataFrame({'A': ['apple', 'xyz', 'aa'],
'B': ['app', 'b', 'aa']})
df_subset = df[df['A'].str.startswith(df['B'])]
但它出錯了,我發現的這個解決方案也沒有幫助。
KeyError: "None of [Float64Index([nan, nan, nan], dtype='float64')] are in the [columns]"
np.where(df['A'].str.startswith(df['B']), True, False)
從這里也為所有人返回True
。
對於逐行比較,我們可以使用DataFrame.apply
:
m = df.apply(lambda x: x['A'].startswith(x['B']), axis=1)
df[m]
A B
0 apple app
2 aa aa
您的代碼不起作用的原因是Series.str.startswith
接受character sequence
(字符串標量),並且您使用的是 pandas Series
。 引用文檔:
拍:str
字符序列。 不接受正則表達式。
您可能需要使用 for 循環,因為str.startswith
不支持行檢查
[x.startswith(y) for x , y in zip(df.A,df.B)]
Out[380]: [True, False, True]
df_sub=df[[x.startswith(y) for x , y in zip(df.A,df.B)]].copy()
您可以在不使用 for 循環的情況下實現此目的:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['apple', 'xyz', 'aa'],
'B': ['app', 'b', 'aa']})
ufunc = np.frompyfunc(str.startswith, 2, 1)
idx = ufunc(df['A'], df['B'])
df[idx]
Out[22]:
A B
0 apple app
2 aa aa
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.