给定此示例:
df = pd.DataFrame(data=[(1, 'A', 5.0, 0.0),
(2, 'A', 3.0, 0.0),
(1, 'B', 1.0, 0.0),
(2, 'B', 2.0, 0.0),
(1, 'C', 6.0, 2.0)],
columns=('Date', 'Name', 'limit', 'allocation')).set_index(['Date','Name'])
groups = df.groupby('Date')
group = groups.get_group(1)
groupSorted = group.sort_values(by='limit', ascending=False)
现在groupSorted
看起来是这样的:
limit allocation
Date Name
1 C 6.0 2.0
A 5.0 0.0
B 1.0 0.0
产生了groupSorted
子数据帧之后,我想要一个公式set
第一行的allocation
满足allocation
为0的条件。我该怎么做
我找到了各种方法来选择第一行。例如
groupSorted[groupSorted.allocation == 0].iloc[0]
groupSorted[groupSorted.allocation == 0].head(1)
。。。甚至是我想要改变的获取值的方法:
groupSorted[groupSorted.allocation == 0].iat[0, 1]
groupSorted[groupSorted.allocation == 0].iloc[0].at['allocation']
groupSorted[groupSorted.allocation == 0].iloc[0]['allocation']
groupSorted[groupSorted.allocation == 0]['allocation'].iat[0]
但是没有一个允许我设置这个值如何设置该值以使其“固定”在原始group
和df
?
从评论到问题:
要分配给
groupSorted
,我们可以使用:要将赋值“粘贴”到原始数据帧中,我们可以执行以下操作:
相关问题 更多 >
编程相关推荐