我想重复使用一行的熊猫列值,并在随后的所有行中重复使用此值。
df = pd.DataFrame({'A' : [1,2,3,4,5,6],
'B' : [2,3,4,5,6,7]})
df.loc[df.A < df.B, 'C'] = df.B.shift(1)
print(df)
给我:
A B C
0 1 2 NaN
1 2 3 2.0
2 3 4 3.0
3 4 5 4.0
4 5 6 5.0
5 6 7 6.0
但是我真正想要的是:
A B C
0 1 2 NaN
1 2 3 2.0
2 3 4 2.0
3 4 5 2.0
4 5 6 2.0
5 6 7 2.0
感谢您的帮助!
答案 0 :(得分:0)
SuperStew在这里是正确的;也许像这样:
df['C'] = 2
df['C'] = df['C'].shift(1)
答案 1 :(得分:0)
感谢您的回答。我想要的是在满足特定条件之前从行中获取值。
df = pd.DataFrame({'A' : [1,2,3,4,5,6],
'B' : [2,3,4,5,6,7]})
df['C'] = df['A'] * df['B']
df.loc[df.A < df.B, 'C'] = df['C'].shift(1)
print(df)
例如:
A B C
0 1 2 2.0
1 2 3 2.0 # if A<B take the value from C from line above
2 3 4 2.0 # if A is still < B take again the value from C from
3 4 5 2.0 # line above (which originally was 2 lines above)
4 5 6 2.0 # and so on
5 6 7 2.0
但是我得到的是:
A B C
0 1 2 NaN
1 2 3 2.0
2 3 4 6.0
3 4 5 12.0
4 5 6 20.0
5 6 7 30.0
答案 2 :(得分:0)
您可以使用idxmax。如果条件为True,则获取C列中的最新值,否则C = A *B。
df = pd.DataFrame({'A' : [1,2,3,4,5,6],'B' : [2,3,4,5,6,7]})
df['C'] = df['A'] * df['B']
df['C'] = np.where(df.A < df.B, df.loc[(df.A < df.B).idxmax(), 'C'], df.C)
A B C
0 1 2 2
1 2 3 2
2 3 4 2
3 4 5 2
4 5 6 2
5 6 7 2