我创建了一个循环来将sqlite数据库读入pandas数据帧,我试图根据"Code"
将它们合并在一起
...
df = pandas.Dataframe() # Creating an empty dataframe for merging at the end
items = ["tb1", "tb2", "tb3"]
for each_item in items:
my_value = pandas.read_sql_query('select "Code", "Name", "Value" from {tb_name} where "Value" is not null'
.format(tbl_name='"%s"' % each_item), con=engine)
print(my_value)
# This below code is my attempt to merge the dataframes that was obtained through the for loop
merge_value = pandas.merge(my_value, df, on='Code', how='outer')
my_value结果:
# tb1 results
Code Name Value
0 C01 Name1 0.010000
1 C02 Name2 0.001200
2 C03 Name3 0.000300
3 C04 Name4 0.001700
# tb2 results
Code Name Value
0 C03 Name3 0.010000
1 C04 Name4 0.001200
2 C05 Name5 0.000300
3 C06 Name6 0.001700
# tb3 results
Code Name Value
0 C01 Name1 0.010000
1 C02 Name2 0.001200
2 C05 Name5 0.000300
3 C06 Name6 0.001700
我正在尝试将它们合并到一个表中,如下所示:
# desired results
Code Name Value_x Value_y Value_Z
0 C01 Name1 0.010000 NULL 0.010000
1 C02 Name2 0.001200 NULL 0.001200
2 C03 Name3 0.000300 0.010000 NULL
3 C04 Name4 0.001700 0.001200 NULL
4 C05 Name5 NULL 0.000300 0.000300
5 C06 Name6 NULL 0.001700 0.001700
如何合并?我尝试了下面的内容,但它产生了key error: Code
merge_value = pandas.merge(my_value, df, on='Code', how='outer')
答案 0 :(得分:3)
您可以使用DataFrame.merge而不是pandas.merge
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html
使用列创建一个空的DataFrame,以防止出现"键错误:代码"
df = pd.DataFrame(columns=['Code'])
然后在循环中,你
df.merge(my_value, on='Code', how='outer')
创建my_value后