我有一个像这样的数据列:
df['zone'].unique()
out[4]:
array(['BROOKLYN', 'BRONX', '07 BRONX', 'Unspecified', '05 BRONX',
'QUEENS', 'MANHATTAN', '07 MANHATTAN', 'STATEN ISLAND',
'17 BROOKLYN', '0 Unspecified', 'Unspecified MANHATTAN',
'12 BROOKLYN', '07 BROOKLYN', '09 MANHATTAN', '01 STATEN ISLAND',
'12 MANHATTAN', '04 QUEENS', '06 BROOKLYN',
'01/04/2016 01:45:00 PM', '01/02/2016 05:43:34 AM', '07 QUEENS',
'11 BRONX', '01/04/2016 03:45:00 PM', '10 MANHATTAN', '03 BRONX',
'04 BRONX', ' or 311 Online."', '01/13/2016 12:00:00 AM',
'04 BROOKLYN', '03 BROOKLYN', '01 QUEENS',
'01/04/2016 03:34:55 PM', '08 MANHATTAN', '14 BROOKLYN',
'10 QUEENS', 'Unspecified STATEN ISLAND', '02 BRONX', '09 BRONX',
'08 QUEENS', '10 BRONX', '03 MANHATTAN', '12 QUEENS',
' please call (212) NEW-YORK (212-639-9675)."',
'Unspecified BROOKLYN', '01/11/2016 04:45:00 PM', '04 MANHATTAN',
'01 BRONX', '09 BROOKLYN', '01/05/2016 07:00:00 AM', '18 BROOKLYN',
'01/08/2016 09:00:00 AM', '01 BROOKLYN', '06 BRONX',
'01 MANHATTAN', '01/06/2016 12:15:00 PM', '02/04/2016 08:45:00 PM',
'01/05/2016 12:45:00 PM', ' no action was taken."', '05 BROOKLYN',
'08 BROOKLYN', 'Unspecified QUEENS', '01/08/2016 03:00:00 PM',
'08/22/2016 12:00:00 AM', '13 BROOKLYN', '02 QUEENS', '14 QUEENS',
'01/05/2016 08:45:00 AM', '11 QUEENS', '02 MANHATTAN',
'01/08/2016 10:05:00 AM', '01/05/2016 01:05:00 PM',
'Unspecified BRONX', '06 QUEENS', '09 QUEENS', '15 BROOKLYN',
'01/07/2016 09:25:00 AM', '02 STATEN ISLAND',
'01/02/2016 12:00:00 PM', '01/06/2016 08:45:00 PM',
'04/04/2016 12:00:00 AM', '01/06/2016 08:30:00 AM'])
如您所见,我在那里有很多混合类型,所有东西都被熊猫归类为字符串对象。我已经在pd.read_csv
命令中尝试过一些参数,例如low_memory = False
,chunksize
等,都没有成功。
尽管如此,我真正需要做的是将该列映射为以下格式:
(Manhattan -> 1, Brooklyn -> 2, Queens -> 3, Staten Island -> 4, Bronx -> 5, Other -> 0)
我还需要包含字符串'07 BRONX'作为bronx,而不是其他或未知字符串。
我已经考虑过使用.map()
方法,但是由于该列确实是混合类型的混乱,因此我不确定我的选择是什么。
在这里,我将不胜感激。
非常感谢
答案 0 :(得分:3)
通过字典的extract
键创建值的字典,其中|
到map
的OR
,最后fillna
的所有不匹配值都映射到{{1} }:
0
a = np.array(['BROOKLYN', 'BRONX', '07 BRONX', 'Unspecified', '05 BRONX',
'QUEENS', 'MANHATTAN', '07 MANHATTAN', 'STATEN ISLAND',
'17 BROOKLYN', '0 Unspecified', 'Unspecified MANHATTAN',
'12 BROOKLYN', '07 BROOKLYN', '09 MANHATTAN', '01 STATEN ISLAND',
'12 MANHATTAN', '04 QUEENS', '06 BROOKLYN',
'01/04/2016 01:45:00 PM', '01/02/2016 05:43:34 AM', '07 QUEENS',
'11 BRONX', '01/04/2016 03:45:00 PM', '10 MANHATTAN', '03 BRONX',
'04 BRONX', ' or 311 Online."', '01/13/2016 12:00:00 AM',
'04 BROOKLYN', '03 BROOKLYN', '01 QUEENS',
'01/04/2016 03:34:55 PM', '08 MANHATTAN', '14 BROOKLYN',
'10 QUEENS', 'Unspecified STATEN ISLAND', '02 BRONX', '09 BRONX',
'08 QUEENS', '10 BRONX', '03 MANHATTAN', '12 QUEENS',
' please call (212) NEW-YORK (212-639-9675)."',
'Unspecified BROOKLYN', '01/11/2016 04:45:00 PM', '04 MANHATTAN',
'01 BRONX', '09 BROOKLYN', '01/05/2016 07:00:00 AM', '18 BROOKLYN',
'01/08/2016 09:00:00 AM', '01 BROOKLYN', '06 BRONX',
'01 MANHATTAN', '01/06/2016 12:15:00 PM', '02/04/2016 08:45:00 PM',
'01/05/2016 12:45:00 PM', ' no action was taken."', '05 BROOKLYN',
'08 BROOKLYN', 'Unspecified QUEENS', '01/08/2016 03:00:00 PM',
'08/22/2016 12:00:00 AM', '13 BROOKLYN', '02 QUEENS', '14 QUEENS',
'01/05/2016 08:45:00 AM', '11 QUEENS', '02 MANHATTAN',
'01/08/2016 10:05:00 AM', '01/05/2016 01:05:00 PM',
'Unspecified BRONX', '06 QUEENS', '09 QUEENS', '15 BROOKLYN',
'01/07/2016 09:25:00 AM', '02 STATEN ISLAND',
'01/02/2016 12:00:00 PM', '01/06/2016 08:45:00 PM',
'04/04/2016 12:00:00 AM', '01/06/2016 08:30:00 AM'])
df=pd.DataFrame({ 'zone':a })