1

'reobsmaf' 是一个 DataFrame,如下所示:

In [152]: reobsmaf[:5]
Out[152]: 
          yy  mm  dd  count      mean    median       min       max       std
2001-01  2001   1  15     76       NaN       NaN       NaN       NaN       NaN
2001-02  2001   2  15    672  5.410384  5.388889  4.111111  6.611111  0.484927
2001-03  2001   3  15    744  3.956317  3.666667  2.888889  5.555556  0.612632
2001-04  2001   4  15    720  3.685185  3.666667  3.333333  3.833333  0.118767
2001-05  2001   5  15    744  3.846550  3.833333  3.666667  4.444444  0.175195

'remodmaf' 是另一个 DataFrame,如下所示:

In [153]: remodmaf[:5]
Out[153]: 
          yy  mm  dd  count      mean    median       min       max       std
2001-01  2001   1  15     75       NaN       NaN       NaN       NaN       NaN
2001-02  2001   2  15    673       NaN       NaN       NaN       NaN       NaN
2001-03  2001   3  15    745  4.508178  4.601572  3.117909  6.313059  0.925472
2001-04  2001   4  15    721  5.402154  5.510775  4.015624  5.923597  0.469602
2001-05  2001   5  15    745  5.352246  5.311082  5.104457  5.892380  0.186441

现在我想比较这两个DataFrame,如果reobsmaf中哪个记录的'mean'值等于'nan',remodmaf中记录的'mean'值等于'nan'。如下:

        for i in range(len(reobsmaf)):
          if str(reobsmaf['mean'][i])=='nan'
             remodmaf['mean'][i]= 'nan'

它引发了一个例外:

DateParseError: day is out of range for month

我知道这是一个特定的问题,但我真的不知道问题是什么。

4

1 回答 1

1

如果正确阅读您的问题,您想在一帧中分配 NaN,而另一帧是 Nan?

In [4]: df = pd.DataFrame(np.random.randn(8,3),columns=['A','B','C'])
In [9]: df2 = df.copy()

In [10]: df2.ix[0:3,['B','C']] = np.nan

In [11]: df2
Out[11]: 
          A         B         C
0  0.404500       NaN       NaN
1  1.391802       NaN       NaN
2 -0.365778       NaN       NaN
3  0.693149       NaN       NaN
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364

In [12]: df
Out[12]: 
          A         B         C
0  0.404500 -0.222471  0.864739
1  1.391802 -0.503080  0.307754
2 -0.365778  0.530288 -0.743119
3  0.693149 -0.749732 -1.135363
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364

这是一个 where 操作。其中 df2 为 nan,将 df 设置为 nan

In [14]: df[pd.isnull(df2)] = np.nan

In [15]: df
Out[15]: 
          A         B         C
0  0.404500       NaN       NaN
1  1.391802       NaN       NaN
2 -0.365778       NaN       NaN
3  0.693149       NaN       NaN
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364
于 2013-03-20T01:10:04.033 回答