[英]How to select specific rows in a pandas df
I am trying to sort a pandas
df
based on specific values. 我试图根据具体的值对
pandas
df
进行排序。 So for the pandas
df
below I want to select values A, C
in Column Event
. 所以对于下面的
pandas
df
,我想在Column Event
选择值A, C
。 I also want to select values U,Z
in Column Code
我还想在
Column Code
选择值U,Z
.
import pandas as pd
d = ({
'Event' : ['A','B','C','D','E','A','B','C','D'],
'Code' : ['W','X','Y','U','Z','X','Y','W','Z'],
'Int' : [1,2,3,4,5,6,7,8,9]
})
df = pd.DataFrame(data = d)
I can do it via one column: 我可以通过一栏完成:
df = df.loc[df['Event'].isin(['A','C'])]
But if I try to include the second Column 但是,如果我尝试包括第二列
df = df.loc[df['Code'].isin(['U','Z'])]
It returns an empty df. 它返回一个空的df。 My intended df is:
我的意图是:
Event Code Int
0 A W 1
1 C Y 3
2 D U 4
3 E Z 5
4 A X 6
5 C W 8
6 D Z 9
I think you need: 我想你需要:
df = df.loc[df['Event'].isin(['A','C']) | df['Code'].isin(['U','Z'])].reset_index(drop=True)
Output: 输出:
Code Event Int
0 W A 1
1 Y C 3
2 U D 4
3 Z E 5
4 X A 6
5 W C 8
6 Z D 9
What's happening here is u are first selecting the rows with A,C and in that u are trying to search for columns with U and Z. But if you notice, none of the rows with A,C in Event have a value of U and Z in code column. 这里发生的事情是你首先选择带有A,C的行,并且你正试图用U和Z搜索列。但是如果你注意到,事件中没有A,C的行的值都没有U和代码栏中的Z. That is the reason you are getting an empty dataframe.
这就是你得到一个空数据帧的原因。
Try the below: 试试以下内容:
newdf = df.query("Event in ['A','C'] | Code in ['U','Z']")
newdf
Event Code Int
0 A W 1
2 C Y 3
3 D U 4
4 E Z 5
5 A X 6
7 C W 8
8 D Z 9
一种可能的解决方
df[(df.Code.isin(['U','Z'])) | (df.Event.isin(['A', 'C']))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.