在Python中两列相减时得到NaN。

huangapple go评论57阅读模式
英文:

Getting NaN when subtracting two columns in Python

问题

我在计算数据框中基于两个其他列计算新列时遇到了意外的结果。结果中所有行都出现了NaN值,而应该是数值,并且介于-3到3之间。

这是代码:

df_kro_emu['Metro_Misc'] = (df_kro_emu['Numeric_Category'] - df_kro_bom_metro['Numeric_Category'])
print(df_kro_emu.head().to_dict())
print(df_kro_bom_metro.head().to_dict())

{ 'Date_Time': { 13063: Timestamp('2023-02-21 10:00:00'), 13064: Timestamp('2023-02-21 10:01:00'), 13065: Timestamp('2023-02-21 10:02:00'), 13066: Timestamp('2023-02-21 10:03:00'), 13067: Timestamp('2023-02-21 10:04:00') }, 'Tg (°C)': { 13063: 40.61538461538461, 13064: 40.75833333333334, 13065: 39.958333333333336, 13066: 39.43333333333333, 13067: 39.958333333333336 }, 'Tg (C) HSRI': { 13063: 44.34615384615385, 13064: 44.02499999999999, 13065: 43.675, 13066: 43.90833333333333, 13067: 44.016666666666666 }, 'Ta (C)': { 13063: 29.346153846153847, 13064: 29.024999999999995, 13065: 28.675, 13066: 28.908333333333335, 13067: 29.016666666666666 }, 'RH (%)': { 13063: 35.276923076923076, 13064: 34.449999999999996, 13065: 34.625, 13066: 35.03333333333334, 13067: 35.14166666666667 }, 'WS': { 13063: 1.4876923076923076, 13064: 2.4458333333333333, 13065: 2.620833333333333, 13066: 0.8741666666666666, 13067: 1.5433333333333337 }, 'Tr (C)': { 13063: 76.38746179131067, 13064: 85.48178010929809, 13065: 86.7072432205415, 13066: 68.15367060286684, 13067: 76.76176725962466 }, 'm': { 13063: 80, 13064: 80, 13065: 80, 13066: 80, 13067: 80 }, 'Ht': { 13063: 1.8, 13064: 1.8, 13065: 1.8, 13066: 1.8, 13067: 1.8 }, 'PB': { 13063: 101.9, 13064: 101.9, 13065: 101.9, 13066: 101.9, 13067: 101.9 }, 'vcomb': { 13063: 1.7876923076923077, 13064: 2.745833333333333, 13065: 2.920833333333333, 13066: 1.1741666666666666, 13067: 1.8433333333333337 }, 'vself': { 13063: 0.3, 13064: 0.3, 13065: 0.3, 13066: 0.3, 13067: 0.3 }, 'VO2': { 13063: 14, 13064: 14, 13065: 14, 13066: 14, 13067: 14 }, 'VO2.1': { 13063: 1.12, 13064: 1.12, 13065: 1.12, 13066: 1.12, 13067: 1.12 }, 'RER': { 13063: 0.8, 13064: 0.8, 13065: 0.8, 13066: 0.8, 13067: 0.8 }, 'Tcl': { 13063:

英文:

I am recieving unexpected results when computing a new column based off two other columns within my data frame. I am getting NaN values across all rows when the results should be numeric and between -3 to 3.

This is the code:

df_kro_emu['Metro_Misc'] = (df_kro_emu['Numeric_Category'] - df_kro_bom_metro['Numeric_Category'])
print(df_kro_emu.head().to_dict())
print(df_kro_bom_metro.head().to_dict())
{'Date_Time': {13063: Timestamp('2023-02-21 10:00:00'), 13064: Timestamp('2023-02-21 10:01:00'), 13065: Timestamp('2023-02-21 10:02:00'), 13066: Timestamp('2023-02-21 10:03:00'), 13067: Timestamp('2023-02-21 10:04:00')}, 'Tg (ºC)': {13063: 40.61538461538461, 13064: 40.75833333333334, 13065: 39.958333333333336, 13066: 39.43333333333333, 13067: 39.958333333333336}, 'Tg (C) HSRI': {13063: 44.34615384615385, 13064: 44.02499999999999, 13065: 43.675, 13066: 43.90833333333333, 13067: 44.016666666666666}, 'Ta (C)': {13063: 29.346153846153847, 13064: 29.024999999999995, 13065: 28.675, 13066: 28.908333333333335, 13067: 29.016666666666666}, 'RH (%)': {13063: 35.276923076923076, 13064: 34.449999999999996, 13065: 34.625, 13066: 35.03333333333334, 13067: 35.14166666666667}, 'WS': {13063: 1.4876923076923076, 13064: 2.4458333333333333, 13065: 2.620833333333333, 13066: 0.8741666666666666, 13067: 1.5433333333333337}, 'Tr (C)': {13063: 76.38746179131067, 13064: 85.48178010929809, 13065: 86.7072432205415, 13066: 68.15367060286684, 13067: 76.76176725962466}, 'm': {13063: 80, 13064: 80, 13065: 80, 13066: 80, 13067: 80}, 'Ht': {13063: 1.8, 13064: 1.8, 13065: 1.8, 13066: 1.8, 13067: 1.8}, 'PB': {13063: 101.9, 13064: 101.9, 13065: 101.9, 13066: 101.9, 13067: 101.9}, 'vcomb': {13063: 1.7876923076923077, 13064: 2.745833333333333, 13065: 2.920833333333333, 13066: 1.1741666666666666, 13067: 1.8433333333333337}, 'vself': {13063: 0.3, 13064: 0.3, 13065: 0.3, 13066: 0.3, 13067: 0.3}, 'VO2': {13063: 14, 13064: 14, 13065: 14, 13066: 14, 13067: 14}, 'VO2.1': {13063: 1.12, 13064: 1.12, 13065: 1.12, 13066: 1.12, 13067: 1.12}, 'RER': {13063: 0.8, 13064: 0.8, 13065: 0.8, 13066: 0.8, 13067: 0.8}, 'Tcl': {13063: 36, 13064: 36, 13065: 36, 13066: 36, 13067: 36}, 'Tsk': {13063: 36, 13064: 36, 13065: 36, 13066: 36, 13067: 36}, 'ε': {13063: 0.95, 13064: 0.95, 13065: 0.95, 13066: 0.95, 13067: 0.95}, 'Ar/AD': {13063: 0.35, 13064: 0.35, 13065: 0.35, 13066: 0.35, 13067: 0.35}, 'Icl': {13063: 1.03, 13064: 1.03, 13065: 1.03, 13066: 1.03, 13067: 1.03}, 'Re,cl': {13063: 0.023, 13064: 0.023, 13065: 0.023, 13066: 0.023, 13067: 0.023}, 'AD': {13063: 1.9917607971689137, 13064: 1.9917607971689137, 13065: 1.9917607971689137, 13066: 1.9917607971689137, 13067: 1.9917607971689137}, 'AD:m': {13063: 248.9700996461142, 13064: 248.9700996461142, 13065: 248.9700996461142, 13066: 248.9700996461142, 13067: 248.9700996461142}, 'Ta': {13063: 302.49615384615385, 13064: 302.17499999999995, 13065: 301.825, 13066: 302.05833333333334, 13067: 302.16666666666663}, 'Pa': {13063: 1.4414226533641852, 13064: 1.3817708157865098, 13065: 1.3609337187971684, 13066: 1.3957178897153115, 13067: 1.4088342507596854}, 'Pa.1': {13063: 10.810669900231389, 13064: 10.363281118398824, 13065: 10.207002890978764, 13066: 10.467884172864837, 13067: 10.56625688069764}, 'Psa': {13063: 4.086021477046317, 13064: 4.010945764256923, 13065: 3.9304944947210636, 13066: 3.9839711409571215, 13067: 4.0090137560152295}, 'PB.1': {13063: 764.25, 13064: 764.25, 13065: 764.25, 13066: 764.25, 13067: 764.25}, 'Abs Humid': {13063: 0.010340254307468287, 13064: 0.00992286810707943, 13065: 0.00978456446546792, 13066: 0.01002689708063749, 13067: 0.010117496935957586}, 'v': {13063: 6.435692307692308, 13064: 9.885, 13065: 10.514999999999999, 13066: 4.226999999999999, 13067: 6.636000000000002}, 'M (W)': {13063: 376.959744, 13064: 376.959744, 13065: 376.959744, 13066: 376.959744, 13067: 376.959744}, 'M(w/m2)': {13063: 189.25954589316655, 13064: 189.25954589316655, 13065: 189.25954589316655, 13066: 189.25954589316655, 13067: 189.25954589316655}, 'H (W)': {13063: 376.959744, 13064: 376.959744, 13065: 376.959744, 13066: 376.959744, 13067: 376.959744}, 'H (W/m2)': {13063: 189.25954589316655, 13064: 189.25954589316655, 13065: 189.25954589316655, 13066: 189.25954589316655, 13067: 189.25954589316655}, 'fcl': {13063: 1.3193000000000001, 13064: 1.3193000000000001, 13065: 1.3193000000000001, 13066: 1.3193000000000001, 13067: 1.3193000000000001}, 'hc': {13063: 8.232880188170252, 13064: 10.650760920741826, 13065: 11.053000715687972, 13066: 6.39755429891934, 13067: 8.385683335817054}, 'hr': {13063: 2.695136016522989, 13064: 2.808300006313886, 13065: 2.823786818231824, 13066: 2.595339088602694, 13067: 2.699732540401542}, 'h': {13063: 10.928016204693241, 13064: 13.459060927055711, 13065: 13.876787533919796, 13066: 8.992893387522034, 13067: 11.085415876218597}, 'to': {13063: 40.94777676952506, 13064: 40.80498797959874, 13065: 40.48397834156718, 13066: 40.23449427752332, 13067: 40.644466936395744}, 'σ': {13063: 5.67e-08, 13064: 5.67e-08, 13065: 5.67e-08, 13066: 5.67e-08, 13067: 5.67e-08}, 'Rcl': {13063: 0.15965000000000001, 13064: 0.15965000000000001, 13065: 0.15965000000000001, 13066: 0.15965000000000001, 13067: 0.15965000000000001}, 'C + R (W)': {13063: -43.03194799740991, 13064: -44.31405834649421, 13065: -41.68072728300708, 13066: -34.57500862544615, 13067: -40.56845420599385}, 'W/m2)': {13063: -21.604977896229038, 13064: -22.248684887001566, 13065: -20.926572780351943, 13066: -17.35901654184128, 13067: -20.368135703673754}, 'Ps,sk': {13063: 5.94114568997784, 13064: 5.94114568997784, 13065: 5.94114568997784, 13066: 5.94114568997784, 13067: 5.94114568997784}, 'he': {13063: 135.84252310480917, 13064: 175.73755519224014, 13065: 182.37451180885154, 13066: 105.55964593216912, 13067: 138.3637750409814}, 'LR': {13063: 16.5, 13064: 16.5, 13065: 16.5, 13066: 16.5, 13067: 16.5}, 'λ': {13063: 2427, 13064: 2427, 13065: 2427, 13066: 2427, 13067: 2427}, 'Ereq': {13063: 388.67999800708145, 13064: 389.403607749275, 13065: 386.4496792925081, 13066: 379.693942434434, 13067: 385.83009732657763}, 'Emax': {13063: 282.2317601326206, 13064: 299.2358537475848, 13065: 302.34090075233314, 13066: 269.9772147554108, 13067: 285.29071606806275}, 'Ereq.1': {13063: 195.14391414850152, 13064: 195.50721567708973, 13065: 194.0241417753614, 13066: 190.6323003114282, 13067: 193.71306929777714}, 'Emax.1': {13063: 141.6996260463528, 13064: 150.23684278399207, 13065: 151.79578852143294, 13066: 135.54700701969637, 13067: 143.2354309179971}, 'ωreq': {13063: 1.3771660490103625, 13064: 1.3013267055817102, 13065: 1.278191863326735, 13066: 1.4063925460466078, 13067: 1.3524102804471523}, 'H.S.I.': {13063: 137.71660490103625, 13064: 130.13267055817101, 13065: 127.81918633267351, 13066: 140.63925460466078, 13067: 135.24102804471522}, 'r': {13063: 0.5, 13064: 0.5, 13065: 0.5, 13066: 0.5, 13067: 0.5}, 'Sreq': {13063: 390.28782829700305, 13064: 391.01443135417946, 13065: 388.0482835507228, 13066: 381.2646006228564, 13067: 387.4261385955543}, 'Sreq.1': {13063: 1153.0679792546298, 13064: 1155.2146583414833, 13065: 1146.4514589641774, 13066: 1126.409717976071, 13067: 1144.6133913273009}, 'Slimit': {13063: 1153.0679792546298, 13064: 1155.2146583414833, 13065: 1146.4514589641774, 13066: 1126.409717976071, 13067: 1144.6133913273009}, 'Cres + Eres': {13063: 31.311693990328443, 13064: 31.870194597219218, 13065: 32.190791990498944, 13066: 31.840810191012135, 13067: 31.69810087941621}, 'Cres + Eres.1': {13063: 15.72060964089405, 13064: 16.001015103078377, 13065: 16.161976898157096, 13066: 15.98626212357961, 13067: 15.914612299063146}, 'Heat': {13063: 0.0, 13064: 0.0, 13065: 0.0, 13066: 0.0, 13067: 0.0}, 'Sweat Rate': {13063: 1.530679792546299, 13064: 1.552146583414833, 13065: 1.4645145896417744, 13066: 1.2640971797607103, 13067: 1.4461339132730089}, 'Black Globe Temp': {13063: 4.846153846153847, 13064: 4.5249999999999915, 13065: 4.174999999999997, 13066: 4.408333333333331, 13067: 4.516666666666666}, 'HSS Score': {13063: 6.376833638700146, 13064: 6.077146583414825, 13065: 5.639514589641772, 13066: 5.672430513094042, 13067: 5.962800579939675}, 'Sunny Factor': {13063: 4, 13064: 4, 13065: 4, 13066: 4, 13067: 4}, 'Td (C)': {13063: 16.401538461538465, 13064: 15.914999999999992, 13065: 15.600000000000001, 13066: 15.915000000000001, 13067: 16.045}, 'WBGT (C)': {13063: 24.874442734333602, 13064: 24.453539087131027, 13065: 24.12923627755603, 13066: 24.406119207913576, 13067: 24.526657268840502}, 'e (hPa)': {13063: 10.929194665558198, 13064: 10.321537117381753, 13065: 10.001300960702363, 13066: 10.369196457795358, 13067: 10.519611371095424}, 'Category': {13063: 'Moderate Risk', 13064: 'Moderate Risk', 13065: 'Moderate Risk', 13066: 'Moderate Risk', 13067: 'Moderate Risk'}, 'Stadium': {13063: 'Karen Rolton Oval', 13064: 'Karen Rolton Oval', 13065: 'Karen Rolton Oval', 13066: 'Karen Rolton Oval', 13067: 'Karen Rolton Oval'}, 'City': {13063: 'Adelaide', 13064: 'Adelaide', 13065: 'Adelaide', 13066: 'Adelaide', 13067: 'Adelaide'}, 'State': {13063: 'SA', 13064: 'SA', 13065: 'SA', 13066: 'SA', 13067: 'SA'}, 'BOM_station_id': {13063: 2300, 13064: 2300, 13065: 2300, 13066: 2300, 13067: 2300}, 'BOM_station': {13063: 'Adelaide', 13064: 'Adelaide', 13065: 'Adelaide', 13066: 'Adelaide', 13067: 'Adelaide'}, 'BOM2_station_id': {13063: 23034, 13064: 23034, 13065: 23034, 13066: 23034, 13067: 23034}, 'BOM2_station': {13063: 'Adelaide Airport', 13064: 'Adelaide Airport', 13065: 'Adelaide Airport', 13066: 'Adelaide Airport', 13067: 'Adelaide Airport'}, 'Measurement_device': {13063: 'EMU', 13064: 'EMU', 13065: 'EMU', 13066: 'EMU', 13067: 'EMU'}, 'Longitude': {13063: 138.585269, 13064: 138.585269, 13065: 138.585269, 13066: 138.585269, 13067: 138.585269}, 'Latitude': {13063: -34.92304, 13064: -34.92304, 13065: -34.92304, 13066: -34.92304, 13067: -34.92304}, 'Day': {13063: 'Day 1', 13064: 'Day 1', 13065: 'Day 1', 13066: 'Day 1', 13067: 'Day 1'}, 'Numeric_Category': {13063: 2, 13064: 2, 13065: 2, 13066: 2, 13067: 2}, 'venue_type': {13063: 'Minor', 13064: 'Minor', 13065: 'Minor', 13066: 'Minor', 13067: 'Minor'}, 'Metro_Misc': {13063: nan, 13064: nan, 13065: nan, 13066: nan, 13067: nan}, 'Airport_Misc': {13063: nan, 13064: nan, 13065: nan, 13066: nan, 13067: nan}}
{'Date_Time': {13434: Timestamp('2023-02-21 10:00:00'), 13435: Timestamp('2023-02-21 10:01:00'), 13436: Timestamp('2023-02-21 10:02:00'), 13437: Timestamp('2023-02-21 10:03:00'), 13438: Timestamp('2023-02-21 10:04:00')}, 'Tg (ºC)': {13434: nan, 13435: nan, 13436: nan, 13437: nan, 13438: nan}, 'Tg (C) HSRI': {13434: 43.3, 13435: 43.3, 13436: 43.4, 13437: 43.4, 13438: 43.2}, 'Ta (C)': {13434: 28.3, 13435: 28.3, 13436: 28.4, 13437: 28.4, 13438: 28.2}, 'RH (%)': {13434: 33.0, 13435: 33.0, 13436: 33.0, 13437: 33.0, 13438: 33.0}, 'WS': {13434: 2.7083333333333335, 13435: 2.333333333333333, 13436: 2.7083333333333335, 13437: 2.333333333333333, 13438: 2.7083333333333335}, 'Tr (C)': {13434: 87.16551332318113, 13435: 84.00339566216257, 13436: 87.23325791341932, 13437: 84.07295565984703, 13438: 87.09779476193177}, 'm': {13434: 80, 13435: 80, 13436: 80, 13437: 80, 13438: 80}, 'Ht': {13434: 1.8, 13435: 1.8, 13436: 1.8, 13437: 1.8, 13438: 1.8}, 'PB': {13434: 101.9, 13435: 101.9, 13436: 101.9, 13437: 101.9, 13438: 101.9}, 'vcomb': {13434: 3.0083333333333333, 13435: 2.633333333333333, 13436: 3.0083333333333333, 13437: 2.633333333333333, 13438: 3.0083333333333333}, 'vself': {13434: 0.3, 13435: 0.3, 13436: 0.3, 13437: 0.3, 13438: 0.3}, 'VO2': {13434: 14, 13435: 14, 13436: 14, 13437: 14, 13438: 14}, 'VO2.1': {13434: 1.12, 13435: 1.12, 13436: 1.12, 13437: 1.12, 13438: 1.12}, 'RER': {13434: 0.8, 13435: 0.8, 13436: 0.8, 13437: 0.8, 13438: 0.8}, 'Tcl': {13434: 36, 13435: 36, 13436: 36, 13437: 36, 13438: 36}, 'Tsk': {13434: 36, 13435: 36, 13436: 36, 13437: 36, 13438: 36}, 'ε': {13434: 0.95, 13435: 0.95, 13436: 0.95, 13437: 0.95, 13438: 0.95}, 'Ar/AD': {13434: 0.35, 13435: 0.35, 13436: 0.35, 13437: 0.35, 13438: 0.35}, 'Icl': {13434: 1.03, 13435: 1.03, 13436: 1.03, 13437: 1.03, 13438: 1.03}, 'Re,cl': {13434: 0.023, 13435: 0.023, 13436: 0.023, 13437: 0.023, 13438: 0.023}, 'AD': {13434: 1.9917607971689137, 13435: 1.9917607971689137, 13436: 1.9917607971689137, 13437: 1.9917607971689137, 13438: 1.9917607971689137}, 'AD:m': {13434: 248.9700996461142, 13435: 248.9700996461142, 13436: 248.9700996461142, 13437: 248.9700996461142, 13438: 248.9700996461142}, 'Ta': {13434: 301.45, 13435: 301.45, 13436: 301.54999999999995, 13437: 301.54999999999995, 13438: 301.34999999999997}, 'Pa': {13434: 1.2691326932730778, 13435: 1.2691326932730778, 13436: 1.2765292112889488, 13437: 1.2765292112889488, 13438: 1.2617734608101485}, 'Pa.1': {13434: 9.518495199548083, 13435: 9.518495199548083, 13436: 9.573969084667116, 13437: 9.573969084667116, 13438: 9.463300956076115}, 'Psa': {13434: 3.8458566462820536, 13435: 3.8458566462820536, 13436: 3.868270337239239, 13437: 3.868270337239239, 13438: 3.8235559418489347}, 'PB.1': {13434: 764.25, 13435: 764.25, 13436: 764.25, 13437: 764.25, 13438: 764.25}, 'Abs Humid': {13434: 0.009135902950414925, 13435: 0.009135902950414925, 13436: 0.00918609977946284, 13437: 0.00918609977946284, 13438: 0.00908594129735531}, 'v': {13434: 10.83, 13435: 9.479999999999999, 13436: 10.83, 13437: 9.479999999999999, 13438: 10.83}, 'M (W)': {13434: 376.959744, 13435: 376.959744, 13436: 376.959744, 13437: 376.959744, 13438: 376.959744}, 'M(w/m2)': {13434: 189.25954589316655, 13435: 189.25954589316655, 13436: 189.25954589316655, 13437: 189.25954589316655, 13438: 189.25954589316655}, 'H (W)': {13434: 376.959744, 13435: 376.959744, 13436: 376.959744, 13437: 376.959744, 13438: 376.959744}, 'H (W/m2)': {13434: 189.25954589316655, 13435: 189.25954589316655, 13436: 189.25954589316655, 13437: 189.25954589316655, 13438: 189.25954589316655}, 'fcl': {13434: 1.3193000000000001, 13435: 1.3193000000000001, 13436: 1.3193000000000001, 13437: 1.3193000000000001, 13438: 1.3193000000000001}, 'hc': {13434: 11.250496940844023, 13435: 10.38674893990695, 13436: 11.250496940844023, 13437: 10.38674893990695, 13438: 11.250496940844023}, 'hr': {13434: 2.8295928093672127, 13435: 2.7896923849225574, 13436: 2.8304517648995047, 13437: 2.790566050571995, 13438: 2.8287343575601276}, 'h': {13434: 14.080089750211236, 13435: 13.176441324829508, 13436: 14.080948705743527, 13437: 13.177314990478944, 13438: 14.079231298404151}, 'to': {13434: 40.12985592946837, 13435: 40.09342243191553, 13436: 40.22624141141152, 13437: 40.189887402071, 13438: 40.03347386632434}, 'σ': {13434: 5.67e-08, 13435: 5.67e-08, 13436: 5.67e-08, 13437: 5.67e-08, 13438: 5.67e-08}, 'Rcl': {13434: 0.15965000000000001, 13435: 0.15965000000000001, 13436: 0.15965000000000001, 13437: 0.15965000000000001, 13438: 0.15965000000000001}, 'C + R (W)': {13434: -38.53081287294796, 13435: -37.54165987398798, 13436: -39.430678620952136, 13437: -38.427035761835185, 13438: -37.63100697400266}, 'W/m2)': {13434: -19.345100540042566, 13435: -18.848478154279192, 13436: -19.796894625599045, 13437: -19.292997340069814, 13438: -18.893336502802608}, 'Ps,sk': {13434: 5.94114568997784, 13435: 5.94114568997784, 13436: 5.94114568997784, 13437: 5.94114568997784, 13438: 5.94114568997784}, 'he': {13434: 185.63319952392638, 13435: 171.38135750846467, 13436: 185.63319952392638, 13437: 171.38135750846467, 13438: 185.63319952392638}, 'LR': {13434: 16.5, 13435: 16.5, 13436: 16.5, 13437: 16.5, 13438: 16.5}, 'λ': {13434: 2427, 13435: 2427, 13436: 2427, 13437: 2427, 13438: 2427}, 'Ereq': {13434: 382.50318948115637, 13435: 381.5140364821964, 13436: 383.5040652723237, 13437: 382.50042241320676, 13438: 381.50261669318604}, 'Emax': {13434: 309.23150535747214, 13435: 305.4025450606127, 13436: 308.74194413363466, 13437: 304.91904567218484, 13438: 309.7185987226645}, 'Ereq.1': {13434: 192.04273426048243, 13435: 191.54611187471906, 13436: 192.5452422888511, 13437: 192.04134500332188, 13438: 191.54037836042028}, 'Emax.1': {13434: 155.2553428087416, 13435: 153.33294313991493, 13436: 155.00954962688294, 13437: 153.09019341358479, 13438: 155.49989695695294}, 'ωreq': {13434: 1.2369476681846567, 13435: 1.2492169520279472, 13436: 1.2421508400761037, 13437: 1.2544327021947617, 13438: 1.2317717381732056}, 'H.S.I.': {13434: 123.69476681846567, 13435: 124.92169520279472, 13436: 124.21508400761037, 13437: 125.44327021947616, 13438: 123.17717381732056}, 'r': {13434: 0.5, 13435: 0.5, 13436: 0.5, 13437: 0.5, 13438: 0.5}, 'Sreq': {13434: 384.08546852096487, 13435: 383.0922237494381, 13436: 385.0904845777022, 13437: 384.08269000664376, 13438: 383.08075672084055}, 'Sreq.1': {13434: 1134.7437017982388, 13435: 1131.809255324192, 13436: 1137.712925406152, 13437: 1134.7354929440003, 13438: 1131.7753770873258}, 'Slimit': {13434: 1134.7437017982388, 13435: 1131.809255324192, 13436: 1137.712925406152, 13437: 1134.7354929440003, 13438: 1131.7753770873258}, 'Cres + Eres': {13434: 32.98736739179158, 13435: 32.98736739179158, 13436: 32.88635734862842, 13437: 32.88635734862842, 13438: 33.08813428081661}, 'Cres + Eres.1': {13434: 16.561912172726654, 13435: 16.561912172726654, 13436: 16.51119822991448, 13437: 16.51119822991448, 13438: 16.61250403554887}, 'Heat': {13434: 0.0, 13435: 0.0, 13436: 0.0, 13437: 0.0, 13438: 0.0}, 'Sweat Rate': {13434: 1.3474370179823891, 13435: 1.3180925532419208, 13436: 1.3771292540615199, 13437: 1.3473549294400033, 13438: 1.3177537708732578}, 'Black Globe Temp': {13434: 3.799999999999997, 13435: 3.799999999999997, 13436: 3.8999999999999986, 13437: 3.8999999999999986, 13438: 3.700000000000003}, 'HSS Score': {13434: 5.147437017982386, 13435: 5.118092553241918, 13436: 5.277129254061519, 13437: 5.247354929440002, 13438: 5.017753770873261}, 'Sunny Factor': {13434: 4, 13435: 4, 13436: 4, 13437: 4, 13438: 4}, 'Td (C)': {13434: 14.9, 13435: 14.9, 13436: 14.999999999999998, 13437: 14.999999999999998, 13438: 14.799999999999999}, 'WBGT (C)': {13434: 23.587766361746002, 13435: 23.587766361746002, 13436: 23.68245408935036, 13437: 23.68245408935036, 13438: 23.493446992380267}, 'e (hPa)': {13434: 9.16454544973537, 13435: 9.16454544973537, 13436: 9.261206334224843, 13437: 9.261206334224843, 13438: 9.068821863563027}, 'Category': {13434: 'Moderate Risk', 13435: 'Moderate Risk', 13436: 'Moderate Risk', 13437: 'Moderate Risk', 13438: 'Moderate Risk'}, 'Stadium': {13434: 'Karen Rolton Oval', 13435: 'Karen Rolton Oval', 13436: 'Karen Rolton Oval', 13437: 'Karen Rolton Oval', 13438: 'Karen Rolton Oval'}, 'City': {13434: 'Adelaide', 13435: 'Adelaide', 13436: 'Adelaide', 13437: 'Adelaide', 13438: 'Adelaide'}, 'State': {13434: 'SA', 13435: 'SA', 13436: 'SA', 13437: 'SA', 13438: 'SA'}, 'BOM_station_id': {13434: 2300, 13435: 2300, 13436: 2300, 13437: 2300, 13438: 2300}, 'BOM_station': {13434: 'Adelaide', 13435: 'Adelaide', 13436: 'Adelaide', 13437: 'Adelaide', 13438: 'Adelaide'}, 'BOM2_station_id': {13434: 23034, 13435: 23034, 13436: 23034, 13437: 23034, 13438: 23034}, 'BOM2_station': {13434: 'Adelaide Airport', 13435: 'Adelaide Airport', 13436: 'Adelaide Airport', 13437: 'Adelaide Airport', 13438: 'Adelaide Airport'}, 'Measurement_device': {13434: 'BOM Adelaide', 13435: 'BOM Adelaide', 13436: 'BOM Adelaide', 13437: 'BOM Adelaide', 13438: 'BOM Adelaide'}, 'Longitude': {13434: 138.585269, 13435: 138.585269, 13436: 138.585269, 13437: 138.585269, 13438: 138.585269}, 'Latitude': {13434: -34.92304, 13435: -34.92304, 13436: -34.92304, 13437: -34.92304, 13438: -34.92304}, 'Day': {13434: 'Day 1', 13435: 'Day 1', 13436: 'Day 1', 13437: 'Day 1', 13438: 'Day 1'}, 'Numeric_Category': {13434: 2, 13435: 2, 13436: 2, 13437: 2, 13438: 2}, 'venue_type': {13434: 'Minor', 13435: 'Minor', 13436: 'Minor', 13437: 'Minor', 13438: 'Minor'}}

All values in the Numeric_Category are Int and present. No NaN values are in the column of each df.

I expect values between -3 and 3 as the values in the Numeric_Category column are between 1 and 4

I receive Nan's in each row so see the output

答案1

得分: 1

感谢添加数据框字典。我发现两个数据框中“Numeric_Category”列中的值类型不同。

type(df_kro_emu.head()["Numeric_Category"])pd.Series 类型,但 type(df_kro_bom_airport.head()["Numeric_Category"])dict 类型。因此,从 Python 字典中减去一个 pd.Series 可能是错误输出的原因。

减法操作的输出是:

df_kro_emu["Numeric_Category"]

13063    2
13064    2
13065    2
13066    2
13067    2
Name: Numeric_Category, dtype: int64

****

df_kro_bom_airport["Numeric_Category"]
{13434: 2, 13435: 2, 13436: 2, 13437: 2, 13438: 2}

这个输出中字典的键与 df_kro_emu 的输出不匹配。因此,即使我们有两个系列,也可能不会发生减法,因为 df_kro_emu 中没有索引 13063 的元素存在于 df_kro_bom_airport 中。

注意到这些问题,一个快速的修复方法应该是:

df_kro_emu["Numeric_Category"].values - list(df_kro_bom_airport["Numeric_Category"].values())

如果你想保留 df_kro_emu 中的索引:

df_kro_emu["Numeric_Category"] - list(df_kro_bom_airport["Numeric_Category"].values())

或者,如果你想保留 df_kro_bom_airport 中的索引:

pd.Series(df_kro_bom_airport["Numeric_Category"]) - df_kro_emu["Numeric_Category"].values
英文:

Thanks for adding the dataframe dictionaries. I found that the type of values in the "Numeric_Category" column of both dataframes is different.

type(df_kro_emu.head()["Numeric_Category"]) is of type pd.Series but type(df_kro_bom_airport.head()["Numeric_Category"]) is dict. Hence, subtracting a pd.Series from a python dict could be the reason of the wrong output.

The output of -

df_kro_emu["Numeric_Category"]
13063    2
13064    2
13065    2
13066    2
13067    2
Name: Numeric_Category, dtype: int64

AND

df_kro_bom_airport["Numeric_Category"]
{13434: 2, 13435: 2, 13436: 2, 13437: 2, 13438: 2}

The keys of the dictionary inn this output do not match with the output from df_kro_emu. Hence, even if we have two series, the subtraction may not happen as there is no element on index 13063 from df_kro_emu in df_kro_bom_airport.

Noting these problems, a quick fix should be-

df_kro_emu["Numeric_Category"].values - list(df_kro_bom_airport["Numeric_Category"].values())

If you want to preserve the indices in df_kro_emu -

df_kro_emu["Numeric_Category"] - list(df_kro_bom_airport["Numeric_Category"].values())

Or, preserving the indices in df_kro_bom_airport -

pd.Series(df_kro_bom_airport["Numeric_Category"]) - df_kro_emu["Numeric_Category"].values

答案2

得分: 0

df_kro_emu['Numeric_Category'] 表示一个系列,在您的图片中,您的两个具有不同索引的 df,因此 2 个系列相减可能导致 NaN,您可以查看 https://pandas.pydata.org/docs/reference/api/pandas.Series.subtract.html

>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a    1.0
b    1.0
c    1.0
d    NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a    1.0
b    NaN
d    1.0
e    NaN
dtype: float64
>>> a.subtract(b, fill_value=0)
a    0.0
b    1.0
c    1.0
d   -1.0
e    NaN
dtype: float64
英文:

df_kro_emu['Numeric_Category'] represent a series, In you picture, your two df with different index, so 2 series subtract may leads to NaN, you can see https://pandas.pydata.org/docs/reference/api/pandas.Series.subtract.html

>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a    1.0
b    1.0
c    1.0
d    NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a    1.0
b    NaN
d    1.0
e    NaN
dtype: float64
>>> a.subtract(b, fill_value=0)
a    0.0
b    1.0
c    1.0
d   -1.0
e    NaN
dtype: float64

huangapple
  • 本文由 发表于 2023年7月18日 14:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定