私はpandasの初心者ですが、少なくとも以下のコードが動作します。次のように結果の端部は、
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、私はこの行 `DF [ '結果'] = res.reset_index(あなたのコード、
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]
DropEdit = True) 'ValueErrorのために私のために働かない:1を渡したアイテムの数が間違っている、インデックスが3を意味する ' – SparkAndShine
@sparkandshineが奇妙です。どのバージョンのPythonを使用していますか?これは2.7.3でうまく動作します。 – ASGM
私のPythonのバージョンは '2.7.6'です。 – SparkAndShine