我有一个包含这样的数据框。(一个例子)
col1 col2 col3
0 1 2 1
1 2 4 2
2 3 6 3
3 4 nan nan <------
4 5 nan nan
5 6 12 nan
如何用第一列行索引3值替换行索引3?
我想要的最终结果是:
col1 col2 col3
0 1 2 1
1 2 4 2
2 3 6 3
3 4 4 4 <-----
4 5 nan nan
5 6 12 nan
您可以使用iloc方法访问具有索引的行
>>> df = pd.DataFrame({'Col1': [1,2,3,4,5,6], 'Col2':[0,0,0,0,0,0]})
>>> df
Col1 Col2
0 1 0
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
>>> df.loc[0]
Col1 1
Col2 0
Name: 0, dtype: int64
>>>
>>> df.iloc[0]
Col1 1
Col2 0
Name: 0, dtype: int64
>>>
>>>
>>>
>>> df['Col2'].iloc[1] = df['Col1'].iloc[1]
>>> df
Col1 Col2
0 1 0
1 2 2
2 3 0
3 4 0
4 5 0
5 6 0
>>>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句