0

我有一个需要更正的 DataFrame。我想将包含该单词的任何列中的空值更改TRAINER0.

我的数据框看起来像这样:

ID,ENROLLMENT_DATE,TRAINER_MANAGING,TRAINER_OPERATOR,FIRST_VISIT_DATE
1536D,12-Feb-12,"06DA1B3-Lebanon NH",,15-Feb-12
F15D,18-May-12,"06405B2-Lebanon NH",,25-Jul-12
8096,8-Aug-12,"0643D38-Hanover NH","0643D38-Hanover NH",25-Jun-12
A036,1-Apr-12,"06CB8CF-Hanover NH","06CB8CF-Hanover NH",9-Aug-12
8944,19-Feb-12,"06D26AD-Hanover NH",,4-Feb-12
1004E,8-Jun-12,"06388B2-Lebanon NH",,24-Dec-11
11795,3-Jul-12,"0649597-White River VT","0649597-White River VT",30-Mar-12
30D7,11-Nov-12,"06D95A3-Hanover NH","06D95A3-Hanover NH",30-Nov-11
3AE2,21-Feb-12,"06405B2-Lebanon NH",,26-Oct-12
B0FE,17-Feb-12,"06D1B9D-Hartland VT",,16-Feb-12
127A1,11-Dec-11,"064456E-Hanover NH","064456E-Hanover NH",11-Nov-12
161FF,20-Feb-12,"0643D38-Hanover NH","0643D38-Hanover NH",3-Jul-12
A036,30-Nov-11,"063B208-Randolph VT","063B208-Randolph VT",
475B,25-Sep-12,"06D26AD-Hanover NH",,5-Nov-12
151A3,7-Mar-12,"06388B2-Lebanon NH",,16-Nov-12
CA62,3-Jan-12,,,
D31B,18-Dec-11,"06405B2-Lebanon NH",,9-Jan-12
20F5,8-Jul-12,"0669C50-Randolph VT",,3-Feb-12
8096,19-Dec-11,"0649597-White River VT","0649597-White River VT",9-Apr-12
14E48,1-Aug-12,"06D3206-Hanover NH",,
177F8,20-Aug-12,"063B208-Randolph VT","063B208-Randolph VT",5-May-12
553E,11-Oct-12,"06D95A3-Hanover NH","06D95A3-Hanover NH",8-Mar-12
12D5F,18-Jul-12,"0649597-White River VT","0649597-White River VT",2-Nov-12
C6DC,13-Apr-12,"06388B2-Lebanon NH",,
11795,27-Feb-12,"0643D38-Hanover NH","0643D38-Hanover NH",19-Jun-12
17B43,11-Aug-12,,,22-Oct-12
A036,11-Aug-12,"06D3206-Hanover NH",,19-Jun-12

以下代码为我提供了正确的值。

numpy.where(test.filter(regex='TRAINER') == '', 0, test.filter(regex='TRAINER'))

我只是不确定如何用新的更正数据覆盖我的 DataFrame 中的原始数据?非常感谢任何帮助!

4

1 回答 1

3

我不会numpy在这里使用;我认为这只会让事情变得更复杂。这个怎么样 - 首先,读入文件:

>>> df = pd.read_csv("train.csv")
>>> df[:4]
      ID ENROLLMENT_DATE    TRAINER_MANAGING    TRAINER_OPERATOR FIRST_VISIT_DATE
0  1536D       12-Feb-12  06DA1B3-Lebanon NH                 NaN        15-Feb-12
1   F15D       18-May-12  06405B2-Lebanon NH                 NaN        25-Jul-12
2   8096        8-Aug-12  0643D38-Hanover NH  0643D38-Hanover NH        25-Jun-12
3   A036        1-Apr-12  06CB8CF-Hanover NH  06CB8CF-Hanover NH         9-Aug-12

然后选择要修复的列:

>>> to_fix = [c for c in df.columns if 'TRAINER' in c]
>>> to_fix
['TRAINER_MANAGING', 'TRAINER_OPERATOR']

然后使用该.fillna方法将NaNs 替换为 0,然后分配列:

>>> df[to_fix] = df[to_fix].fillna(0)
>>> df[:4]
      ID ENROLLMENT_DATE    TRAINER_MANAGING    TRAINER_OPERATOR FIRST_VISIT_DATE
0  1536D       12-Feb-12  06DA1B3-Lebanon NH                   0        15-Feb-12
1   F15D       18-May-12  06405B2-Lebanon NH                   0        25-Jul-12
2   8096        8-Aug-12  0643D38-Hanover NH  0643D38-Hanover NH        25-Jun-12
3   A036        1-Apr-12  06CB8CF-Hanover NH  06CB8CF-Hanover NH         9-Aug-12

或者,你可以简单地做

df.fillna({c: 0 for c in df.columns if 'TRAINER' in c}, inplace=True)

但这似乎有点太神奇了。

于 2013-02-05T19:22:03.863 回答