Skip to main content
 首页 » 编程设计

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

2024年10月01日9lvdongjie

我想通过 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)