使用字母数字列表中的 .loc 和 asin 过滤 DataFrame
我正在尝试使用包含字母数字值的列表来过滤数据框。下面是一个示例数据框:
A | B |
---|---|
D0102 | 52 |
Patient | 150 |
D5175 | 71 |
DaysWorked | 12 |
我创建了一个名为“codes”的列表:
codes = ['D0102', 'D5175']
我使用的代码如下所示,数据框没有提取任何内容:
new_df= df.loc[df['A'].isin(codes)]
所需的输出我要显示的数据框是这样的:
A | B |
---|---|
D0102 | 52 |
D5175 | 71 |
当我将名为“codes”的列表更改为:
codes = ['Patients', 'DaysWorked']
I使用同一行代码:
new_df= df.loc[df['A'].isin(codes)]
新数据框提取了正确的数据:
A | B |
---|---|
患者 | 150 |
DaysWorked | 12 |
I am trying to filter a dataframe using a list with alphanumerical values. Here is a sample dataframe below:
A | B |
---|---|
D0102 | 52 |
Patients | 150 |
D5175 | 71 |
DaysWorked | 12 |
I created a list named "codes" to:
codes = ['D0102', 'D5175']
The code I used is shown below and the dataframe didn't pull up anything:
new_df= df.loc[df['A'].isin(codes)]
The desired output dataframe that I want to display is this:
A | B |
---|---|
D0102 | 52 |
D5175 | 71 |
When I changed the list named "codes" to:
codes = ['Patients', 'DaysWorked']
I used the same line of code:
new_df= df.loc[df['A'].isin(codes)]
The new dataframe pulled the correct data:
A | B |
---|---|
Patients | 150 |
DaysWorked | 12 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
df
的A
列中很可能存在一些不需要的空格
。你可能想这样做:There's a good chance that the column
A
of yourdf
might have someunwanted whitespaces
in it. You might wanna do this: