我有两列Letter&Values,新列应基于“ Letter”列增加值,如下所示:
import pandas as pd
df = pd.DataFrame(data=[['a', 'one'],
['a', 'two'],
['b', 'three'],
['b', 'four'],
['c', 'five'],
['c', 'five'],
['c', 'five']
],
columns=['Letter', 'value'])
#df['counter'] = df['value'].shift().where(df['Letter'].shift() == df['Letter'], '')
print(df)
df['counter'] = df.apply(lambda x: x+1 if df['Letter'].shift() == df['Letter'] else 1, axis=1)
#print(df)
'''
Expected output
Letter value counter
0 a one p1
1 a two p2
2 b three p1
3 b four p2
4 c five p1
5 c five p2
6 c five p3
'''
您能帮我解决上面的代码吗?谢谢。
要更精确地编辑我的问题,我需要稍后将行转换为列(我的代码可以使用数据透视功能)
答案 0 :(得分:2)
在熊猫中,肯定有一种更优雅的方法,但是我忘记了该函数的名称:
# Create a constant, valued-at-one column for summing each row
>>> df['counter'] = df.assign(ind=1).groupby('Letter')['ind'].cumsum()
>>> df
Letter value counter
0 a one 1
1 a two 2
2 b three 1
3 b four 2
4 c five 1
5 c five 2
6 c five 3