我正在尝试使用以下代码获取子集数据框:
dprev = "eiffel tower"
df.loc[df['place'] == dprev] -> returns empty
drandom = random.choice(df['place'].unique())
df.loc[df['place'] == drandom] -> returns the subset
为什么dprev
是字符串变量时我没有看到同样的事情?
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以尝试将str.contains
与case=False
<强>实施例强>
import pandas as pd
dprev = "eiffel tower"
df = pd.DataFrame({"place": ["eiffel tower", "Eiffel tower", "Hello"], "data":[1,2,3]})
print(df.loc[df['place'].str.contains(dprev, case=False)])
<强>输出:强>
data place
0 1 eiffel tower
1 2 Eiffel tower
答案 2 :(得分:0)
尝试这种方式:
dprev = "eiffel tower"
df = df.loc[df['place'].str.lower() == dprev]
或:
df = df.loc[df['place'].str.lower().str.contains(dprev)]