Python- Pandas : how to divide by specific key's value

标签 python pandas

我想通过 pandas 数据框的其他行来计算列。

例如,当我有这些数据框时,

df = pd.DataFrame({
    "year" : ['2017', '2017', '2017', '2017', '2017','2017', '2017', '2017', '2017'],
    "rooms" : ['1', '2', '3', '1', '2', '3', '1', '2', '3'],
    "city" : ['tokyo', 'tokyo', 'toyko', 'nyc','nyc', 'nyc', 'paris', 'paris', 'paris'],
    "rent" : [1000, 1500, 2000, 1200, 1600, 1900, 900, 1500, 2200],
})

print(df)

    city  rent rooms  year
0  tokyo  1000     1  2017
1  tokyo  1500     2  2017
2  toyko  2000     3  2017
3    nyc  1200     1  2017
4    nyc  1600     2  2017
5    nyc  1900     3  2017
6  paris   900     1  2017
7  paris  1500     2  2017
8  paris  2200     3  2017

我想加上与其他城市同年租金和房间相比的租金。

理想的结果如下,

    city  rent rooms  year  vs_nyc
0  tokyo  1000     1  2017  0.833333
1  tokyo  1500     2  2017  0.9375
2  toyko  2000     3  2017  1.052631
3    nyc  1200     1  2017  1.0
4    nyc  1600     2  2017  1.0
5    nyc  1900     3  2017  1.0
6  paris   900     1  2017  0.75
7  paris  1500     2  2017  0.9375
8  paris  2200     3  2017  1.157894

如何在考虑年份和房间的情况下添加像 vs_nyc 这样的列?

我尝试了一些但没有成功,

# filtering gets NaN value, and fillna(method='pad') also not worked

df.rent / df[df['city'] == 'nyc'].rent

0    NaN
1    NaN
2    NaN
3    1.0
4    1.0
5    1.0
6    NaN
7    NaN
8    NaN
Name: rent, dtype: float64

最佳答案

举例说明:

set_index + unstack

d1 = df.set_index(['city', 'year', 'rooms']).rent.unstack('city')

d1

city           nyc   paris   tokyo   toyko
year rooms                                
2017 1      1200.0   900.0  1000.0     NaN
     2      1600.0  1500.0  1500.0     NaN
     3      1900.0  2200.0     NaN  2000.0

然后我们可以划分

d1.div(d1.nyc, 0)

city        nyc     paris     tokyo     toyko
year rooms                                   
2017 1      1.0  0.750000  0.833333       NaN
     2      1.0  0.937500  0.937500       NaN
     3      1.0  1.157895       NaN  1.052632

解决方案

d1 = df.set_index(['city', 'year', 'rooms']).rent.unstack('city')
df.join(d1.div(d1.nyc, 0).stack().rename('vs_nyc'), on=['year', 'rooms', 'city'])

    city  rent rooms  year    vs_nyc
0  tokyo  1000     1  2017  0.833333
1  tokyo  1500     2  2017  0.937500
2  toyko  2000     3  2017  1.052632
3    nyc  1200     1  2017  1.000000
4    nyc  1600     2  2017  1.000000
5    nyc  1900     3  2017  1.000000
6  paris   900     1  2017  0.750000
7  paris  1500     2  2017  0.937500
8  paris  2200     3  2017  1.157895

有点干净

cols = ['city', 'year', 'rooms']
ny_rent = df.set_index(cols).rent.loc['nyc'].rename('ny_rent')
df.assign(vs_nyc=df.rent / df.join(d1, on=d1.index.names).ny_rent)

关于Python- Pandas : how to divide by specific key's value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008929/

相关文章:

python - 使用 Numpy 和 Pandas 优化 Python 代码

python - 是否使用 Facebook Javascript

Python:im2col 的实现利用了 6 维数组的优势?

pandas - 在 DataFrame 中逐行求和给定列

python - 数据框替换防止在提供 int64 值时替换不正确的 int32 字段

python - 有没有办法在 python pandas 中将基于时间的事件划分为小时模板?

python - 从 Pandas 数据框列中删除 'seconds' 和 'minutes'

python - 使用 Python 的 Apple 推送通知服务

python - 使用 pandas reshape 数据框

python - 如何从github有效地将代码导入ipython