我正在将一个大时间间隔的Pandas DataFrame合并到另一个小时间间隔的DataFrame中,并希望合并列的值保持不变,而不是NaN。
随着日期的增加,我希望总体保持不变直到更新,而不显示NaN。
DataFrame A
DATE, Close, High, Low, Open
959731080000000 106.600 106.600 106.600 106.600
959731380000000 106.650 106.650 106.650 106.650
959732340000000 106.640 106.650 106.640 106.650
959732880000000 106.660 106.660 106.660 106.660
959733180000000 106.670 106.670 106.670 106.670
959733240000000 106.660 106.660 106.660 106.660
959734200000000 106.610 106.610 106.580 106.580
959734320000000 106.600 106.600 106.600 106.600
959735820000000 106.620 106.620 106.620 106.620
959736240000000 106.630 106.640 106.630 106.630
959736540000000 106.620 106.620 106.620 106.620
959736780000000 106.640 106.670 106.640 106.670
959736960000000 106.690 106.690 106.690 106.690
959743320000000 106.690 106.700 106.690 106.700
959745660000000 106.700 106.750 106.250 106.260
959745720000000 106.270 106.280 106.270 106.280
959745900000000 106.270 106.270 106.270 106.270
DataFrame B
DATE, Population
959731080000000 123.456
959732880000000 124.834
959736780000000 125.456
将B合并为A
A = A.merge(B, how='left')
DATE, Close, High, Low, Open, Population
959731080000000 106.600 106.600 106.600 106.600 123.456
959731380000000 106.650 106.650 106.650 106.650 NaN
959732340000000 106.640 106.650 106.640 106.650 NaN
959732880000000 106.660 106.660 106.660 106.660 124.834
959733180000000 106.670 106.670 106.670 106.670 NaN
959733240000000 106.660 106.660 106.660 106.660 NaN
959734200000000 106.610 106.610 106.580 106.580 NaN
959734320000000 106.600 106.600 106.600 106.600 NaN
959735820000000 106.620 106.620 106.620 106.620 NaN
959736240000000 106.630 106.640 106.630 106.630 NaN
959736540000000 106.620 106.620 106.620 106.620 NaN
959736780000000 106.640 106.670 106.640 106.670 125.456
959736960000000 106.690 106.690 106.690 106.690 NaN
959743320000000 106.690 106.700 106.690 106.700 NaN
959745660000000 106.700 106.750 106.250 106.260 NaN
959745720000000 106.270 106.280 106.270 106.280 NaN
959745900000000 106.270 106.270 106.270 106.270 NaN
我希望人口数可以这样填充NaN单元格(因为人口数实际上是恒定的)
DATE, Close, High, Low, Open, Population
959731080000000 106.600 106.600 106.600 106.600 123.456
959731380000000 106.650 106.650 106.650 106.650 123.456
959732340000000 106.640 106.650 106.640 106.650 123.456
959732880000000 106.660 106.660 106.660 106.660 124.834
959733180000000 106.670 106.670 106.670 106.670 124.834
959733240000000 106.660 106.660 106.660 106.660 124.834
959734200000000 106.610 106.610 106.580 106.580 124.834
959734320000000 106.600 106.600 106.600 106.600 124.834
959735820000000 106.620 106.620 106.620 106.620 124.834
959736240000000 106.630 106.640 106.630 106.630 124.834
959736540000000 106.620 106.620 106.620 106.620 124.834
959736780000000 106.640 106.670 106.640 106.670 125.456
959736960000000 106.690 106.690 106.690 106.690 125.456
959743320000000 106.690 106.700 106.690 106.700 125.456
959745660000000 106.700 106.750 106.250 106.260 125.456
959745720000000 106.270 106.280 106.270 106.280 125.456
959745900000000 106.270 106.270 106.270 106.270 125.456
================================================ ================
我尝试将B列的填充数存储为列表,并使用if语句遍历A列的填充数列。它什么也没做。
series = [i for i in B['POPULATION']]
for i in A['POPULATION']:
for k in series:
if i == k:
A['POPULATION'].fillna(k)
答案 0 :(得分:1)
您可以使用merge_asof
:
A = pd.merge_asof(A,B,on='DATE')
print (A)
DATE Close High Low Open Population
0 959731080000000 106.60 106.60 106.60 106.60 123.456
1 959731380000000 106.65 106.65 106.65 106.65 123.456
2 959732340000000 106.64 106.65 106.64 106.65 123.456
3 959732880000000 106.66 106.66 106.66 106.66 124.834
4 959733180000000 106.67 106.67 106.67 106.67 124.834
5 959733240000000 106.66 106.66 106.66 106.66 124.834
6 959734200000000 106.61 106.61 106.58 106.58 124.834
7 959734320000000 106.60 106.60 106.60 106.60 124.834
8 959735820000000 106.62 106.62 106.62 106.62 124.834
9 959736240000000 106.63 106.64 106.63 106.63 124.834
10 959736540000000 106.62 106.62 106.62 106.62 124.834
11 959736780000000 106.64 106.67 106.64 106.67 125.456
12 959736960000000 106.69 106.69 106.69 106.69 125.456
13 959743320000000 106.69 106.70 106.69 106.70 125.456
14 959745660000000 106.70 106.75 106.25 106.26 125.456
15 959745720000000 106.27 106.28 106.27 106.28 125.456
16 959745900000000 106.27 106.27 106.27 106.27 125.456
答案 1 :(得分:0)
您也可以尝试此操作
for i in A['Population']:
if i != NaN:
prev = i
else:
i = prev