我有一个 df:
df = pd.DataFrame({'id': [1, 1, 2, 2, 2, 3, 4, 4, 4], \
"name": ["call", "response", "call", "call", "response", "call", "call", "response", "response"]})
id name
0 1 call
1 1 response
2 2 call
3 2 call
4 2 response
5 3 call
6 4 call
7 4 response
8 4 response
我正在尝试提取一个调用-响应对,其中调用后的第一个响应是正确的模式。调用和响应对位于其自己的 id
子集中,如下所示:
id name
0 1 call
1 1 response
3 2 call
4 2 response
6 4 call
7 4 response
理想情况下,我会将索引
保留在数据框中,以便稍后可以将df.loc
与索引一起使用。
我尝试过的是在子集中遍历df
并应用
某些内容或使用滚动窗口
。但只成功得到错误。
unique_ids = df.id.unique()
for unique_id in unique_ids :
df.query('id== @unique_id').apply(something))
我还没有发现可以专门用于数据帧的子集
的东西
最佳答案
使用DataFrameGroupBy.shift
与 Series.eq
进行比较用于检查 boolean indexing
中的相等性和过滤器:
m1 = df['name'].eq('call') & df.groupby('id')['name'].shift(-1).eq('response')
m2 = df['name'].eq('response') & df.groupby('id')['name'].shift().eq('call')
df2 = df[m1 | m2]
print (df2)
id name
0 1 call
1 1 response
3 2 call
4 2 response
6 4 call
7 4 response
关于python - Pandas 数据帧 : Get value pairs from subsets of dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67487367/