[英]Combining two dataframes in Pandas on multiple columns when one of the target columns do not have matching values?
I have two dataframes like this我有两个这样的数据框
df1 = time, lat, lon, lev, val1
1 10 10 1 10
df2 = time, lat, lon, lev, val2
1 10 10 2 20
where the first four columns are basically coordinates, then I would like to combine/merge them so that the output is this:前四列基本上是坐标,然后我想合并/合并它们,这样 output 就是这样的:
df_total = time, lat, lon, lev, val1, val2
1 10 10 1 10 nan
1 10 10 2 nan 20
I am having trouble since none of the dataframes have matching values in the 'lev' column, but both dataframes have values in 'lev.'我遇到了麻烦,因为没有数据帧在“lev”列中具有匹配值,但是两个数据帧在“lev”中都有值。 When I join on all four columns, the output dataframe is, of course, empty, but when I join on the columns time, lat, and lon, I don't get the behaviour I expect (I get a lev_x and lev_y and it puts the val1 and val2 in the same row).当我加入所有四列时,output dataframe 当然是空的,但是当我加入时间、纬度和经度列时,我没有得到我期望的行为(我得到了 lev_x 和 lev_y 并且它将 val1 和 val2 放在同一行)。 So, how can this be done?那么,如何做到这一点呢?
Use from this code从此代码使用
a = pd.concat([df1, df2], ignore_index=True)
Merely do the following:只需执行以下操作:
import pandas as pd
df1 = pd.DataFrame({'time': [1], 'lat':10, 'lon':10, 'lev':1, 'val1':10})
df2 = pd.DataFrame({'time': [1], 'lat':10, 'lon':10, 'lev':2, 'val2':20})
df = df1.append(df2)
Result结果
time lat lon lev val1 val2
0 1 10 10 1 10.0 NaN
0 1 10 10 2 NaN 20.0
if you absolutely want to convert all non-null elements to integers consider using instead:如果您绝对想将所有非空元素转换为整数,请考虑改用:
df = df1.append(df2).astype('Int64')
# time lat lon lev val1 val2
# 0 1 10 10 1 10 <NA>
# 0 1 10 10 2 <NA> 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.