访问pandas数据帧的单个单元格?

时间:2014-06-25 20:28:01

标签: python numpy pandas

我有以下数据,但有一些漏洞。我已经查看了“如何处理丢失的数据”但在这种情况下找不到任何适用的东西。这是数据:

          Species GearUsed  AverageFishWeight(lbs) NormalRange(lbs)  Caught  
0   BlackBullhead  Gillnet                    0.11          0.8-7.7    0.18   
1    BlackCrappie  Trapnet                    6.22          0.7-3.4    0.30   
2             NaN  Gillnet                    1.00          0.6-3.5    0.30   
3        Bluegill  Trapnet                   11.56         6.1-46.6    0.14   
4             NaN  Gillnet                    1.44              NaN    0.21   
5   BrownBullhead  Trapnet                    0.11          0.4-2.1    1.01   
6    NorthernPike  Trapnet                    0.22              NaN    4.32   
7             NaN  Gillnet                    2.22         3.5-10.5    5.63   
8     Pumpkinseed  Trapnet                    0.89          2.0-8.5    0.23   
9        RockBass  Trapnet                    0.22          0.5-1.8    0.04   
10        Walleye  Trapnet                    0.22          0.3-0.7    0.28   
11            NaN  Gillnet                    1.56          1.3-5.0    2.54   
12    WhiteSucker  Trapnet                    0.33          0.3-1.4    2.76   
13            NaN  Gillnet                    1.78          0.5-2.7    1.32   
14    YellowPerch  Trapnet                    1.33          0.5-3.3    0.14   
15            NaN  Gillnet                   27.67         3.4-43.6    0.14   

我需要物种列中的NaN才能成为它上面的名称,例如第2行将是BlackCrappie。我想迭代框架并手动指定种类名称但不太确定如何,并且其他答案建议不要首先迭代数据框。

如何单独访问框架的每个单元格?谢谢!

PS列名不正确,没有27磅的黄色鲈鱼。 :)

1 个答案:

答案 0 :(得分:1)

您是否也要填写其他行中的缺失值?似乎是fillna()的用途:

In [83]:

print df.fillna(method='pad')
          Species GearUsed  AverageFishWeight(lbs) NormalRange(lbs)  Caught
0   BlackBullhead  Gillnet                    0.11          0.8-7.7    0.18
1    BlackCrappie  Trapnet                    6.22          0.7-3.4    0.30
2    BlackCrappie  Gillnet                    1.00          0.6-3.5    0.30
3        Bluegill  Trapnet                   11.56         6.1-46.6    0.14
4        Bluegill  Gillnet                    1.44         6.1-46.6    0.21
5   BrownBullhead  Trapnet                    0.11          0.4-2.1    1.01
6    NorthernPike  Trapnet                    0.22          0.4-2.1    4.32
7    NorthernPike  Gillnet                    2.22         3.5-10.5    5.63
8     Pumpkinseed  Trapnet                    0.89          2.0-8.5    0.23
9        RockBass  Trapnet                    0.22          0.5-1.8    0.04
10        Walleye  Trapnet                    0.22          0.3-0.7    0.28
11        Walleye  Gillnet                    1.56          1.3-5.0    2.54
12    WhiteSucker  Trapnet                    0.33          0.3-1.4    2.76
13    WhiteSucker  Gillnet                    1.78          0.5-2.7    1.32
14    YellowPerch  Trapnet                    1.33          0.5-3.3    0.14
15    YellowPerch  Gillnet                   27.67         3.4-43.6    0.14