# 基于在同一列的其他值与南栏中将值

##### 问题描述投票：3回答：2

``````values = [[100,54,25,26,32,33,15,2],[1,2,3,4,5,6,7,8]]
columns = ["numbers", "order"]
zipped = dict(zip(columns,values))
df = pd.DataFrame(zipped)
print(df)

numbers  order
0      100      1
1       54      2
2       25      3
3       26      4
4       32      5
5       33      6
6       15      7
7        2      8
``````

``````   numbers  order
0      100      1
1       54      2
2      NaN      3
3      NaN      4
4      NaN      5
5       33      6
6       15      7
7        2      8
``````

``````values = [[100,54,25,26,34,32,31,33,15,2],[1,2,3,4,5,6,7,8,9,10]]

numbers  order
0      100      1
1       54      2
2       25      3
3       26      4
4       34      5
5       32      6
6       31      7
7       33      8
8       15      9
9        2     10
``````

``````   numbers  order
0    100.0      1
1     54.0      2
2      NaN      3
3      NaN      4
4     34.0      5
5      NaN      6
6      NaN      7
7     33.0      8
8     15.0      9
9      2.0     10
``````
python pandas dataframe
##### 2个回答
6

``````In [11]: df.at[3, 'numbers'] = 24  # more illustrative example

In [12]: df.numbers[::-1].cummax()[::-1]
Out[12]:
0    100
1     54
2     33
3     33
4     33
5     33
6     15
7      2
Name: numbers, dtype: int64

In [13]: df.loc[df.numbers < df.numbers[::-1].cummax()[::-1], 'numbers'] = np.nan

In [14]: df
Out[14]:
numbers  order
0    100.0      1
1     54.0      2
2      NaN      3
3      NaN      4
4      NaN      5
5     33.0      6
6     15.0      7
7      2.0      8
``````

1

``````arr = df['numbers'].values
df['numbers'] = [x if all(x > arr[n+1:]) else np.nan for n, x in enumerate(arr)]
df
``````

``````   numbers  order
0    100.0      1
1     54.0      2
2      NaN      3
3      NaN      4
4      NaN      5
5     33.0      6
6     15.0      7
7      2.0      8
``````