根据条件用另一个python pandas dataframe列的值替换另一个python pandas dataframe列的值

时间:2017-06-19 21:19:55

标签: python pandas jupyter-notebook

import pandas as pd
import numpy as np

dt=pd.DataFrame({'schoolNum':['12','13','14','13'],'oldDistrict':['A','B', 'C','B'],'newDistrict':['F',' ','G',' ']})

dt=pd.DataFrame({'schoolNum':['12','13','14','13'],'oldDistrict':['A','B', 'C','B'],'newDistrict':['F','B','G','B']})

我试过了:

dt.ix[dt['oldDistrict']=='13', 'newDistrict']=dt.oldDistrict

但是我收到一条关于轴的错误信息。

2 个答案:

答案 0 :(得分:3)

您可以用np.nan替换空格,然后使用combine_first

dt = dt.replace({' ': np.nan})
dt['newDistrict'] = dt['newDistrict'].combine_first(dt['oldDistrict'])

返回

    newDistrict oldDistrict schoolNum
0   F           A           12
1   B           B           13
2   G           C           14
3   B           B           13

答案 1 :(得分:1)

两件事:

  1. 您想使用.loc而不是.ix:

    dt.loc[<condition>, <column(s) returned>]
    
  2. 'oldDistrict'没有等于“13”的值 - 这些值在“schoolNum”中。所以你想要的是:

    dt.loc[dt['schoolNum']=='13', 'newDistrict'] = dt['oldDistrict']