我正在使用Pandas和numpy,对于以下数据框,将其命名为“ data”,对于具有data ['Borough'] =='Unspecified'的Borough值,我需要在其中使用邮政编码在其左侧的“事件邮政编码”字段中,可以在“事件邮政编码”列上查找匹配的邮政编码和自治市镇。找到后,“未指定”应替换为自治市镇名称。这是测试链接:https://colab.research.google.com/drive/1PgPbS7KxOrNfok3jtKoC55vXAXzK2E_N#scrollTo=poYboz-jhRCN 点击运行时-> Runall
Created Date Complaint Type Incident Zip Borough
0 09/14/2017 04:40:33 PM New Tree Request 11374 QUEENS
1 03/18/2017 10:09:57 AM General Construc 11420 QUEENS
2 03/29/2017 12:38:28 PM General Construc 11230 Unspecified
3 06/05/2017 12:47:55 PM New Tree Request 10028 Unspecified
4 06/15/2017 11:56:36 AM Dead/Dying Tree 10467 BRONX
5 06/19/2017 10:30:46 AM New Tree Request 11230 MANHATTAN
6 06/29/2017 09:25:59 AM New Tree Request 10028 MANHATTAN
7 07/01/2017 12:23:20 PM Damaged Tree 10467 BRONX
8 07/01/2017 11:47:03 AM Damaged Tree 10467 BRONX
9 07/10/2017 10:27:37 AM General Construc 11385 QUEENS
10 07/13/2017 09:20:53 PM General Construc 11385 QUEENS
答案 0 :(得分:2)
IIUC,您想在DataFrame中使用其他值来填充缺失值。您可以使用map
来做到这一点。
首先,生成一个将邮政编码映射到自治市镇的系列。
mapping = (df.query('Borough != "Unspecified"')
.drop_duplicates('Incident Zip')
.set_index('Incident Zip')
.Borough)
mapping
Incident Zip
11374 QUEENS
11420 QUEENS
10467 BRONX
11230 MANHATTAN
10028 MANHATTAN
11385 QUEENS
Name: Borough, dtype: object
现在,将其传递到map
,并使用fillna
将未映射的值填充为“未指定”。
df['Borough'] = df['Incident Zip'].map(mapping).fillna('Unspecified')
df
Created Date Complaint Type Incident Zip Borough
0 09/14/2017 04:40:33 PM New Tree Request 11374 QUEENS
1 03/18/2017 10:09:57 AM General Construc 11420 QUEENS
2 03/29/2017 12:38:28 PM General Construc 11230 MANHATTAN
3 06/05/2017 12:47:55 PM New Tree Request 10028 MANHATTAN
4 06/15/2017 11:56:36 AM Dead/Dying Tree 10467 BRONX
5 06/19/2017 10:30:46 AM New Tree Request 11230 MANHATTAN
6 06/29/2017 09:25:59 AM New Tree Request 10028 MANHATTAN
7 07/01/2017 12:23:20 PM Damaged Tree 10467 BRONX
8 07/01/2017 11:47:03 AM Damaged Tree 10467 BRONX
9 07/10/2017 10:27:37 AM General Construc 11385 QUEENS
答案 1 :(得分:1)
或者:
df.Borough.replace('Unspecified',np.nan,inplace=True)
df.Borough = df.sort_values(by='Incident Zip').groupby('Incident Zip')['Borough'].apply(lambda x : x.ffill().bfill())
>>df
Created Date Complaint Type Incident Zip Borough
0 09/14/2017 04:40:33 PM New Tree Request 11374 QUEENS
1 03/18/2017 10:09:57 AM General Construc 11420 QUEENS
2 03/29/2017 12:38:28 PM General Construc 11230 MANHATTAN
3 2017-05-06 12:47:55 New Tree Request 10028 MANHATTAN
4 06/15/2017 11:56:36 AM Dead/Dying Tree 10467 BRONX
5 06/19/2017 10:30:46 AM New Tree Request 11230 MANHATTAN
6 06/29/2017 09:25:59 AM New Tree Request 10028 MANHATTAN
7 2017-01-07 12:23:20 Damaged Tree 10467 BRONX
8 2017-01-07 11:47:03 Damaged Tree 10467 BRONX
9 2017-10-07 10:27:37 General Construc 11385 QUEENS
10 07/13/2017 09:20:53 PM General Construc 11385 QUEENS