我有一个包含start_year
coulmn的 Df和另一个包含year
和population
如何使用此 csv 将总体值插入到新列中的 Df 中?例子:
DF :
start_year:
400
405
410
412
人口 csv :
年 | 人口 |
---|---|
399 | 5 |
400 | 6 |
404 | 20 |
409 | 30 |
410 | 31 |
德西德 DF:
开始年 | 人口 |
---|---|
400 | 6 |
405 | 20 |
410 | 31 |
412 | 31 |
尝试使用pd.merge_asof()
方法:
#Population=pd.read_csv('Population.csv')
#df=pd.DataFrame({'start_year':[400,405,410,412]})
out=pd.merge_asof(df,Population,left_on='start_year',right_on='Year').drop('Year',1)
的输出out
:
start_year Population
0 400 6
1 405 20
2 410 31
3 412 31
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句