英文:
Comparing subset of rows in pandas
问题
我在想是否有一种好的方法来比较pandas中的一部分行?
假设我有一个带有以下内容的df:
id | in_test | value |
---|---|---|
1 | True | 5 |
2 | True | 5 |
1 | False | 7 |
2 | False | 8 |
我想要的结果是id和从in_test从true到false的差异(或百分比变化)的df。
我知道我可以将表旋转然后执行逐行计算,或者创建一个过滤后的df并将其与另一个过滤后的df合并,然后逐行计算。
我在想是否有一种在一行中完成此操作的python方法?可能使用pandas函数?
百分比差异的输出将是:
id | value |
---|---|
1 | +40% |
2 | +60% |
差异的输出将是:
id | value |
---|---|
1 | 2 |
2 | 3 |
英文:
I was wondering if there is nice way to compare a subset of rows in pandas?
let's say I have a df with:
id | in_test | value |
---|---|---|
1 | True | 5 |
2 | True | 5 |
1 | False | 7 |
2 | False | 8 |
I would like the resulting df with id and difference (or percentage change) from in_test from true to false.
I know I could pivot the table and then perform row wise calculations, or create a filtered df and merge it with another filtered df and then compute it row wise.
I was wondering if there is python way of doing this in one line? With probably a pandas function?
The output for percentage diff would be :
id | value |
---|---|
1 | +40% |
2 | +60% |
The output for diff would be :
id | value |
---|---|
1 | 2 |
2 | 3 |
(or minus -2 & -3 i guess I would have top define some kind of order) )
答案1
得分: 1
按id分组,计算value的差异,然后重置索引
差异
按id分组,计算value的百分比变化,然后重置索引
百分比变化
英文:
df.groupby('id')['value'].apply(lambda x: x.diff().values[1]).reset_index()
difference
df.groupby('id')['value'].apply(lambda x: x.pct_change().values[1] * 100).reset_index()
percentage difference
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论