英文:
How to use Pandas to find relative share of rooms in individual floors
问题
我有一个名为df
的数据框,其结构如下:
Floor Room Area
0 1 Living room 25
1 1 Kitchen 20
2 1 Bedroom 15
3 2 Bathroom 21
4 2 Bedroom 14
我想要添加一个名为floor_share
的系列,用于表示给定楼层的相对份额/比率,以便数据框变成以下形式:
Floor Room Area floor_share
0 1 Living room 18 0.30
1 1 Kitchen 18 0.30
2 1 Bedroom 24 0.40
3 2 Bathroom 10 0.67
4 2 Bedroom 20 0.33
如果可以使用一行代码(或任何其他习惯的方法)来完成此操作,我将非常高兴学习如何实现。
当前的解决方法
我已经找到了一个可以产生正确结果的方法,首先通过以下方式找到每个楼层的总面积:
floor_area_sums = df.groupby('Floor')['Area'].sum()
这将产生以下结果:
Floor
1 60
2 35
Name: Area, dtype: int64
然后,我初始化了一个新的系列为0
,并在遍历数据框行时找到正确的值:
df["floor_share"] = 0
for idx, row in df.iterrows():
df.loc[idx, 'floor_share'] = df.loc[idx, 'Area'] / floor_area_sums[row.Floor]
英文:
I have a dataframe df
with the following structure
Floor Room Area
0 1 Living room 25
1 1 Kitchen 20
2 1 Bedroom 15
3 2 Bathroom 21
4 2 Bedroom 14
and I want to add a series floor_share
with the relative share/ratio of the given floor, so that the dataframe becomes
Floor Room Area floor_share
0 1 Living room 18 0,30
1 1 Kitchen 18 0,30
2 1 Bedroom 24 0,40
3 2 Bathroom 10 0,67
4 2 Bedroom 20 0,33
If it is possible to do this with a one-liner (or any other idiomatic manner), I'll be very happy to learn how.
Current workaround
What I have done that produces the correct results is to first find the total floor areas by
floor_area_sums = df.groupby('Floor')['Area'].sum()
which gives
Floor
1 60
2 35
Name: Area, dtype: int64
I then initialize a new series to 0
, and find the correct values while iterating through the dataframe rows.
df["floor_share"] = 0
for idx, row in df.iterrows():
df.loc[idx, 'floor_share'] = df.loc[idx, 'Area']/floor_area_sums[row.Floor]
答案1
得分: 0
IIUC 使用:
df["floor_share"] = df['Area'].div(df.groupby('Floor')['Area'].transform('sum'))
print(df)
结果:
Floor Room Area floor_share
0 1 Living room 18 0.300000
1 1 Kitchen 18 0.300000
2 1 Bedroom 24 0.400000
3 2 Bathroom 10 0.333333
4 2 Bedroom 20 0.666667
英文:
IIUC use:
df["floor_share"] = df['Area'].div(df.groupby('Floor')['Area'].transform('sum'))
print (df)
Floor Room Area floor_share
0 1 Living room 18 0.300000
1 1 Kitchen 18 0.300000
2 1 Bedroom 24 0.400000
3 2 Bathroom 10 0.333333
4 2 Bedroom 20 0.666667
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论