我的熊猫数据框如下,
Location Loc_Code Service_Onroad Service_Cargo
0 Bangalore Vadakara01 Common Common
1 Bangalore Hosdung02 Transit Transit
2 Bangalore Hosdung02 Common Transit
3 Bangalore Kozhikode Common Common
4 Bangalore Malappuram Transit Transit
5 Bangalore Kalpetta09 Common Common
6 Bangalore Kasargod Transit Transit
7 Bangalore Maananthavaadi Common Common
8 Bangalore Alattur Transit Transit
9 Bangalore Pazhani Common NaN
10 Bangalore Ernakulam Transit Transit
11 Bangalore Aluva Connect Connect
12 Bangalore Kochi Common Transit
13 Bangalore Kochi Transit Transit
14 Bangalore Paravoor Common Common
15 Bangalore Chaavakkaad Transit Transit
16 Bangalore Tirur Transit Transit
17 Bangalore Mannaarkkad Common Common
18 Bangalore Koramangala NaN NaN
19 Bangalore Rajna NaN NaN
20 Bangalore Palai09 Interconnect NaN
21 Bangalore Palai09 Common NaN
我想将Service_Onroad
与Service_Cargo
进行比较,如果匹配,我想在新列中将其写为“ Matched
”(即结果)。如果不匹配,我想将其写为“ Not Matched
”。在任何列(Service_Onroad
和Service_Cargo
中,如果值是NaN,那么我不想在那写任何东西。
我们如何进行比较?我相信在 Pandas 中,Nan也被用作比较的字符串,从而导致“ FALSE”?那么我们如何排除呢?
答案 0 :(得分:0)
尝试一下:
df['Match'] = np.where((df['Service_Onroad']==df['Service_Cargo']), 'Matched', 'Non-Matched')
df['Match'] = np.where((df['Service_Onroad'].isna()) | (df['Service_Cargo'].isna()), '', df['Match'])
print(df)
Location Loc_Code Service_Onroad Service_Cargo Match
0 Bangalore Vadakara01 Common Common Matched
1 Bangalore Hosdung02 Transit Transit Matched
2 Bangalore Hosdung02 Common Transit Non-Matched
3 Bangalore Kozhikode Common Common Matched
4 Bangalore Malappuram Transit Transit Matched
5 Bangalore Kalpetta09 Common Common Matched
6 Bangalore Kasargod Transit Transit Matched
7 Bangalore Maananthavaadi Common Common Matched
8 Bangalore Alattur Transit Transit Matched
9 Bangalore Pazhani Common NaN
10 Bangalore Ernakulam Transit Transit Matched
11 Bangalore Aluva Connect Connect Matched
12 Bangalore Kochi Common Transit Non-Matched
13 Bangalore Kochi Transit Transit Matched
14 Bangalore Paravoor Common Common Matched
15 Bangalore Chaavakkaad Transit Transit Matched
16 Bangalore Tirur Transit Transit Matched
17 Bangalore Mannaarkkad Common Common Matched
18 Bangalore Koramangala NaN NaN
19 Bangalore Rajna NaN NaN
20 Bangalore Palai09 Interconnect NaN
21 Bangalore Palai09 Common NaN
答案 1 :(得分:0)
在结果列中定义要满足的条件:
condition_1=((df['Service_Onroad'].isnull())|(df['Service_Cargo'].isnull()))
condition_2=df['Service_Onroad']==df['Service_Cargo']
然后使用“ np.where”并嵌套条件以获得结果:
df['result']=np.where(condition_1,'',(np.where(condition_2,'match','no_match')))
df.to_excel('PATH',na_rep='')