python - 检查DataFrame中的第n个值是否等于字符串中的第n个字符
问题描述
我有一个df:
df =
c1 c2 c3 c4 c5
0 K 6 nan Y V
1 H nan g 5 nan
2 U B g Y L
还有一根弦
s = 'HKg5'
我想返回 s[0]=c1 的值,s[1]=c2 的值,..... + 在某些情况下 s[i]=nan 的行。
例如,上面 df 中的第 1 行与字符串匹配
row 1=
c1 c2 c3 c4 c5
1 H nan g 5 nan
match=True, regardless of s[1,4]=nan
s = H K g 5
而且字符串长度是动态的,所以我的 df cols 高于 c10
我正在使用 df.apply 但我无法清楚地弄清楚。我想写一个函数传递给df.apply,同时传递字符串。
谢谢你的帮助!
克里斯回答的输出
df=
c1 c2 c3 c4 c5
0 K 6 NaN Y V
1 H NaN g 5 NaN
2 U B g Y L
s = 'HKg5'
s1 = pd.Series(list(s), index=[f'c{x+1}' for x in range(len(s))])
df.loc[((df == s1) | (df.isna())).all(1)]
输出
`c1 c2 c3 c4 c5`
解决方案
从您的字符串创建一个助手Series
并使用布尔逻辑进行过滤:
s1 = pd.Series(list(s), index=[f'c{x+1}' for x in range(len(s))])
# print(s1)
# c1 H
# c2 K
# c3 g
# c4 5
# dtype: object
逻辑df
等于 ( ==
) 此值OR ( |
) 是 nan ( isna
)沿轴 1
使用all
返回所有值都为True
df.loc[((df == s1) | (df.isna())).all(1)]
[出去]
c1 c2 c3 c4 c5
1 H NaN g 5 NaN
所以,作为一个函数,你可以这样做:
def df_match_string(frame, string):
s1 = pd.Series(list(string), index=[f'c{x+1}' for x in range(len(string))])
return ((frame == s1) | (frame.isna())).all(1)
df_match_string(df, s)
[出去]
0 False
1 True
2 False
dtype: bool
更新
我无法使用提供的示例重现您的问题。我的猜测是您的 DataFrame 中的某些值可能有前导/尾随空格?
在尝试上述解决方案之前,请尝试以下预处理步骤:
for col in df:
df[col] = df[col].str.strip()
推荐阅读
- javascript - 在 PREM Dynamics CRM 2016 JavaScript 上相当于我的 C# QueryExpression
- typo3 - 如何通过 Typoscript 正确实例化 tx_news
- php - 打印特定数组数据时出现问题
- java - 使用 gradle 创建可执行 jar
- c# - 当另一个窗口关闭时需要更新一个窗口。需要再次实例化关闭的窗口
- html - 将用 Papaparse 解析的 CSV 渲染成 HTML
- r - 让动画滑块向后(从右到左)模拟倒计时?
- groovy - 更改/选择下拉框中的值.. 使用 Geb
- android - 我的预览在 android studio 中显示为空白
- drop-down-menu - 带有非全宽下拉菜单的 Bootstrap4 固定顶部全宽导航栏