我在名为test.csv的文件中有以下数据集:
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
16D87,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
F4F9,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
C6DC,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
16C99,11-Aug-12,"06D3206-Hanover NH",,19-Jun-12
我可以使用以下代码获取访问过综合体的人员列表:
import numpy as np
a = np.recfromcsv('test.csv')
print a[a['first_visit_date'] != '']
这是我遇到的问题。我似乎找不到打印名为trainer
的条件列的方法。我想弄清楚如果它不为空,如何输出'trainer_operator'
,否则我想输出'trainer_managing'
。我想我需要使用numpy.lib.recfunctions.rec_append_fields
来创建列。我只是不确定如何填充它。
我还应该说我刚刚开始发现numpy的力量,所以我试图将它用于很多事情。这可能不是使用它的正确情况。如果有更好的方法来操纵数据,请告诉我。
提前致谢!
答案 0 :(得分:1)
一种直截了当的方法是使用numpy.where
:
>>> fvd_exists = a['first_visit_date'] != ''
>>> np.where(fvd_exists, a['trainer_managing'], a['trainer_operator'])
array(['"06DA1B3-Lebanon NH"', '"06405B2-Lebanon NH"',
'"0643D38-Hanover NH"', '"06CB8CF-Hanover NH"',
'"06D26AD-Hanover NH"', '"06388B2-Lebanon NH"',
...
如果您有一组更复杂的需求,只需在a
上使用布尔运算表达它们,并将结果传递给fvd_exists
。
然后,您可以使用fromarrays
创建新的记录数组。这是一个完整的例子:
>>> a = np.recfromcsv('test.csv')
>>> b = a[a['first_visit_date'] != '']
>>> has_op = b['trainer_operator'] != ''
>>> trainer_col = np.where(name, b.trainer_operator, b.trainer_managing)
>>> np.rec.fromarrays([b.id, b.enrollment_date, trainer_col],
... names=['id', 'enrollment_date', 'trainer'])
rec.array([('1536D', '12-Feb-12', '"06DA1B3-Lebanon NH"'),
('F15D', '18-May-12', '"06405B2-Lebanon NH"'),
('8096', '8-Aug-12', '"0643D38-Hanover NH"'),
('A036', '1-Apr-12', '"06CB8CF-Hanover NH"'),
('8944', '19-Feb-12', '"06D26AD-Hanover NH"'),
('1004E', '8-Jun-12', '"06388B2-Lebanon NH"'),
('16D87', '3-Jul-12', '"0649597-White River VT"'),
('30D7', '11-Nov-12', '"06D95A3-Hanover NH"'),
('3AE2', '21-Feb-12', '"06405B2-Lebanon NH"'),
('B0FE', '17-Feb-12', '"06D1B9D-Hartland VT"'),
('127A1', '11-Dec-11', '"064456E-Hanover NH"'),
('161FF', '20-Feb-12', '"0643D38-Hanover NH"'),
('475B', '25-Sep-12', '"06D26AD-Hanover NH"'),
('151A3', '7-Mar-12', '"06388B2-Lebanon NH"'),
('D31B', '18-Dec-11', '"06405B2-Lebanon NH"'),
('20F5', '8-Jul-12', '"0669C50-Randolph VT"'),
('C6DC', '19-Dec-11', '"0649597-White River VT"'),
('177F8', '20-Aug-12', '"063B208-Randolph VT"'),
('553E', '11-Oct-12', '"06D95A3-Hanover NH"'),
('12D5F', '18-Jul-12', '"0649597-White River VT"'),
('11795', '27-Feb-12', '"0643D38-Hanover NH"'),
('17B43', '11-Aug-12', ''),
('16C99', '11-Aug-12', '"06D3206-Hanover NH"')],
dtype=[('id', '|S5'), ('enrollment_date', '|S9'), ('trainer', '|S24')])