如何检查Pandas列的字符串值是否包含在另一个Pandas列的字符串值中?
How to check whether the string value of a Pandas Column is contained in the string value of another Pandas Column?
我有一个大数据框,格式如下:
| ID | A | B |
| -------- | ----------------- | ----------------------- |
| 0 | Tenure: Leasehold | Modern;Tenure: Leasehold|
| 1 | First Floor | Refurbished |
| 2 | NaN | Modern |
| 3 | First Floor | NaN |
我想在合并 A 列和 B 列之前删除它们之间的冗余。所以我想检查A列的值是否包含在B列中:如果是,A列应该取B列的值,如果不是,A列的值应该保持不变。
我尝试了以下 lambda 函数:
df['A'] = df['A'].apply(lambda x: df.B if df.A in df.B else df.A)
但是我得到这个错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
然后我尝试使用 np.where 方法,如下所示:
df['A'] = np.where((df.A.values in df.B.values), df.B, df.A)
我可以 运行 代码,但是它 returns 所有列都是错误的,所以我的 DataFrame 没有得到任何修改。
如果我运行下面的代码,它returns不过是真的,所以我知道问题不是出在数据上:
df.loc[0, 'A'] in df.loc[0, 'B']
我尝试修改这段代码并像那样使用它:
df['A'] = np.where((df.loc[:, 'A'] in df.loc[:, 'B']), df.B, df.A)
但是我得到了和上面一样的 TypeError。
我该如何解决这个问题?
df["A"] = df.apply(lambda x: x["B"] if x["A"] in x["B"] else x["A"], axis=1)
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
编辑:处理 NaN
s:
df["A"] = df.apply(
lambda x: x["B"]
if pd.notna(x["A"]) and pd.notna(x["B"]) and x["A"] in x["B"]
else x["A"],
axis=1,
)
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
2 2 NaN Modern
3 3 First Floor NaN
如果要在 "A"
列中填写 NaN
s:
df.loc[df["A"].isna(), "A"] = df.loc[df["A"].isna(), "B"]
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
2 2 Modern Modern
3 3 First Floor NaN
我会使用 zip 进行列表理解,与 pandas 相比,随着数据帧大小的增加,应用会非常快:
df["A"] = [b if a in b else a for a,b in zip(df['A'],df['B'])]
print(df)
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
我有一个大数据框,格式如下:
| ID | A | B |
| -------- | ----------------- | ----------------------- |
| 0 | Tenure: Leasehold | Modern;Tenure: Leasehold|
| 1 | First Floor | Refurbished |
| 2 | NaN | Modern |
| 3 | First Floor | NaN |
我想在合并 A 列和 B 列之前删除它们之间的冗余。所以我想检查A列的值是否包含在B列中:如果是,A列应该取B列的值,如果不是,A列的值应该保持不变。
我尝试了以下 lambda 函数:
df['A'] = df['A'].apply(lambda x: df.B if df.A in df.B else df.A)
但是我得到这个错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
然后我尝试使用 np.where 方法,如下所示:
df['A'] = np.where((df.A.values in df.B.values), df.B, df.A)
我可以 运行 代码,但是它 returns 所有列都是错误的,所以我的 DataFrame 没有得到任何修改。
如果我运行下面的代码,它returns不过是真的,所以我知道问题不是出在数据上:
df.loc[0, 'A'] in df.loc[0, 'B']
我尝试修改这段代码并像那样使用它:
df['A'] = np.where((df.loc[:, 'A'] in df.loc[:, 'B']), df.B, df.A)
但是我得到了和上面一样的 TypeError。
我该如何解决这个问题?
df["A"] = df.apply(lambda x: x["B"] if x["A"] in x["B"] else x["A"], axis=1)
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
编辑:处理 NaN
s:
df["A"] = df.apply(
lambda x: x["B"]
if pd.notna(x["A"]) and pd.notna(x["B"]) and x["A"] in x["B"]
else x["A"],
axis=1,
)
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
2 2 NaN Modern
3 3 First Floor NaN
如果要在 "A"
列中填写 NaN
s:
df.loc[df["A"].isna(), "A"] = df.loc[df["A"].isna(), "B"]
print(df)
打印:
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished
2 2 Modern Modern
3 3 First Floor NaN
我会使用 zip 进行列表理解,与 pandas 相比,随着数据帧大小的增加,应用会非常快:
df["A"] = [b if a in b else a for a,b in zip(df['A'],df['B'])]
print(df)
ID A B
0 0 Modern;Tenure: Leasehold Modern;Tenure: Leasehold
1 1 First Floor Refurbished