首页 > 解决方案 > 如何更改 Pandas 系列中的单个值?

问题描述

import pandas as pd
test_df = pd.Series([0,0,0,1,1,1,1,0,1,1,1,1])
test_df[0] = 1 

如何将此系列中的第一个值更改为一个?它不像列表那样工作。我尝试使用 .replace 方法,但我只想更改一个值,而不是全部。

标签: pythonpandas

解决方案


尝试loc

test_df.loc[0] = 1 
test_df
Out[140]: 
0     1
1     0
2     0
3     1
4     1
5     1
6     1
7     0
8     1
9     1
10    1
11    1
dtype: int64

推荐阅读