Python 如何更改以loc为条件的数据帧中的最后一个值?
我想更改loc最后一行条件输出的值 我尝试了以下方法,但没有成功Python 如何更改以loc为条件的数据帧中的最后一个值?,python,pandas,Python,Pandas,我想更改loc最后一行条件输出的值 我尝试了以下方法,但没有成功 df.loc({condition}, {column}).iloc[-1] = {some_value} 假设我有这个DF: Stake Odds Ftr Ind 0 0.25 2.50 H 1 1 0.15 4.00 D 2 2 0.26 1.75 A 2 3 0.30 2.20 H 1 4 0.10 1.85 H 3 5 0.40
df.loc({condition}, {column}).iloc[-1] = {some_value}
假设我有这个DF:
Stake Odds Ftr Ind
0 0.25 2.50 H 1
1 0.15 4.00 D 2
2 0.26 1.75 A 2
3 0.30 2.20 H 1
4 0.10 1.85 H 3
5 0.40 3.20 A 3
6 0.32 1.50 D 3
7 0.11 1.20 H 1
8 0.20 2.15 H 2
9 0.25 1.65 A 2
当我跑步时:
a = df[df['Ftr']=='H'].tail(1).index.values[0]
df.iloc[a,int(df.columns.get_indexer(['Odds']))] = 10
我得到:
Stake Odds Ftr Ind
0 0.25 2.50 H 1
1 0.15 4.00 D 2
2 0.26 1.75 A 2
3 0.30 2.20 H 1
4 0.10 1.85 H 3
5 0.40 3.20 A 3
6 0.32 1.50 D 3
7 0.11 1.20 H 1
8 0.20 10.00 H 2
9 0.25 1.65 A 2
这就是您要查找的吗?在获取最后一行之前,我想先对数据帧进行条件设置。该条件将应用于什么?该条件将应用于整个数据帧,然后选择一列。我想我可以通过使用df.at[--]来计算。只需抓取最后一行的索引并将其更改即可。我仍然很好奇,如果没有索引,你会怎么做。我不确定我是否理解正确,但请再次检查我的答案并让我知道