pandas 熊猫在循环中合并数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46012279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:22:57  来源:igfitidea点击:

pandas merging dataframes in a loop

pythonpandasmerge

提问by jake wong

I created a loop to read sqlite database into a pandas dataframe, and I am trying to merge them together based on "Code"

我创建了一个循环来将 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 results:

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

I am trying to merge them into one table like below:

我正在尝试将它们合并到一张表中,如下所示:

# 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  

How do I merge it? I tried the below, but it produces key error: Code

我如何合并它?我试过下面的,但它产生key error: Code

merge_value = pandas.merge(my_value, df, on='Code', how='outer')

回答by Jeffrey Ram Pineda

You can use DataFrame.merge instead of pandas.merge

您可以使用 DataFrame.merge 而不是 pandas.merge

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html



Create an empty DataFrame with the columns to prevent the "key error: Code"

使用列创建一个空的 DataFrame 以防止“关键错误:代码”

df = pd.DataFrame(columns=['Code']) 

then in the loop, you

然后在循环中,你

df.merge(my_value, on='Code', how='outer') 

after my_value is created

创建 my_value 后