python - Python pandas: groupby and devide by the first value of each group
问题描述
I have a pandas dataframe like this.
>data
ID Distance Speed
1 100 40
1 200 20
1 200 10
2 400 20
2 500 30
2 100 40
2 600 20
2 700 90
3 800 80
3 700 10
3 400 20
I want to groupby the table by ID
, and create a new column time
by dividing each value in the Distance
column by the first row of the Speed
column of each ID
group. So the result should look like this.
>data
ID Distance Speed Time
1 100 40 2.5
1 200 20 5
1 200 10 5
2 400 20 20
2 500 30 25
2 100 40 5
2 600 20 30
2 700 90 35
3 800 80 10
3 700 10 8.75
3 400 20 5
My attempt:
data['Time'] = data['Distance'] / data.loc[data.groupby('ID')['Speed'].head(1).index, 'Speed']
But the result seems to be not good. How do you do it?
解决方案
使用transform
withfirst
返回与Series
original相同的长度df
:
data['Time'] = data['Distance'] /data.groupby('ID')['Speed'].transform('first')
s = data.drop_duplicates('ID').set_index('ID')['Speed']
data['Time'] = data['Distance'] / data['ID'].map(s)
print (data)
ID Distance Speed Time
0 1 100 40 2.50
1 1 200 20 5.00
2 1 200 10 5.00
3 2 400 20 20.00
4 2 500 30 25.00
5 2 100 40 5.00
6 2 600 20 30.00
7 2 700 90 35.00
8 3 800 80 10.00
9 3 700 10 8.75
10 3 400 20 5.00
推荐阅读
- python - 用于张量流动态 RNN 的动态输入切片
- javascript - 无法读取聊天应用程序中未定义错误的属性“用户名”
- php - 如何使用 laravel 查询生成器获取计数、不同和总国家
- c++ - 删除重复项:如何为 boost::geometry::box 编写排序谓词?
- android - 服务运行应用程序已关闭
- adaptive-cards - 从自适应卡片制作 action.http
- laravel - 需要另一个 composer 包的 Laravel 服务包
- java - 为什么使用“apache httpclient”和“httpurlconnection”时http状态码不同?
- sql-server - 将列作为函数参数传递
- algorithm - 是完全多项式时间逼近方案 多项式时间逼近方案