我正在使用groupby与Python Pandas挣扎。我该如何完成以下操作?对于每种水果,我想找到与该水果的“第0步”值的差异。
df = pd.DataFrame({'Fruit' : ['Apple', 'Apple', 'Apple', 'Banana', 'Banana', 'Banana'], 'Step' : [0, 1, 2, 0, 1, 2], 'Value' : [100, 102, 105, 200, 210, 195] })
Fruit Step Value to-be
0 Apple 0 100 --> 0
1 Apple 1 102 --> 2
2 Apple 2 105 --> 5
3 Banana 0 200 --> 0
4 Banana 1 210 --> 10
5 Banana 2 195 --> -5
谢谢!
答案 0 :(得分:6)
这应该这样做:
df.groupby('Fruit').apply(lambda g: g.Value - g[g.Step == 0].Value.values[0])
首先,我们按照您关注的列(Fruit)进行分组。然后我们将一个函数应用于每个组(使用lambda
,它允许我们在线指定一个函数)。对于每个组,我们找到g.Step == 0
所在的行,然后从该行获取Value条目,并使用values[0]
获取第一个值(如果有多个位置{{1} }})。然后我们只从组中的所有行中减去该值,然后将其返回。
如果要将其作为列添加到数据框中,可以删除索引:
g.Step == 0
答案 1 :(得分:1)
认为这可以解决问题。它只是循环遍历行,并在每次步长等于0时应用新的“第一个”值。然后计算与第一个值的差值。
rows = range(df.shape[0])
df['count'] = 0
for r in rows:
step = df.iloc[r,1]
value = df.iloc[r,2]
if step == 0:
first = value
df.iloc[r,3] = value - first
答案 2 :(得分:1)
我是熊猫新手,但至少以下代码有效。结果结束,
Fruit Step Value to-be
0 Apple 0 100 0
1 Apple 1 102 2
2 Apple 2 105 5
3 Banana 0 200 0
4 Banana 1 210 10
5 Banana 2 195 -5
[6 rows x 4 columns]
源代码如下。
import pandas as pd
df = pd.DataFrame({'Fruit' : ['Apple', 'Apple', 'Apple', 'Banana', 'Banana', 'Banana'],
'Step' : [0, 1, 2, 0, 1, 2],
'Value' : [100, 102, 105, 200, 210, 195] })
list_groups = list()
# loop over dataframe groupby `Fruit`
for name, group in df.groupby('Fruit'):
group.sort('Step', ascending=True) # sorted by `Step`
row_iterator = group.iterrows()
# get the base value
idx, first_row = row_iterator.next()
base_value = first_row['Value']
to_be = [0] # store the values of the column `to-be`
for idx, row in row_iterator:
to_be.append(row['Value'] - base_value)
# add a column to group
group['to-be'] = pd.Series(to_be, index=group.index)
list_groups.append(group)
# Concatenate dataframes
result = pd.concat(list_groups)
print(result)
@ASGM,我运行你的代码,
res = df.groupby('Fruit').apply(lambda g: g.Value - g[g.Step == 0].Value.values[0])
df['Result'] = res.reset_index(drop=True)
但遇到问题,
Traceback (most recent call last):
File "***.py", line 9, in <module>
df['Result'] = res.reset_index(drop=True)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1887, in __setitem__
self._set_item(key, value)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1968, in _set_item
NDFrame._set_item(self, key, value)
File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 1068, in _set_item
self._data.set(key, value)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3024, in set
self.insert(len(self.items), item, value)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3039, in insert
self._add_new_block(item, value, loc=loc)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3162, in _add_new_block
self.items, fastpath=True)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 1993, in make_block
placement=placement)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 64, in __init__
'%d' % (len(items), len(values)))
ValueError: Wrong number of items passed 1, indices imply 3
[Finished in 0.4s with exit code 1]