pandas 熊猫合并df错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46008957/
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:49  来源:igfitidea点击:

Pandas merge df error

pythonpandasmerge

提问by zsad512

I have 3 dataframes I am trying to merge in pandas. One is 20 columns, the other two have 2 columns each. They are organized as such:

我有 3 个数据框,我想在 Pandas 中合并。一个是 20 列,另外两个各有 2 列。它们的组织方式如下:

eth_price.head(n=3)

Out[6]: 
            time  eth_price
0  8/28/17 16:19    344.021
2  8/28/17 16:24    343.833
3  8/28/17 16:29    343.643
btc_price.head(n=3)

Out[7]: 
                  time   btc_price
0  2017-08-27 22:50:00  4,389.6113
1  2017-08-27 22:51:00  4,389.0850
2  2017-08-27 22:52:00  4,388.8625

block_data.head(n=3)
Out[8]: 
                   time  block_size    difficulty  estimated_btc_sent  \
0   2017-08-30 22:55:03   165261989  888171856257      22433058065308   
5   2017-08-30 23:02:03   165261989  888171856257      22433058065308   
12  2017-08-30 23:09:03   164262692  888171856257      22210602766312   

    estimated_transaction_volume_usd     hash_rate  market_price_usd  \
0                       1.030796e+09  7.417412e+09           4594.98   
5                       1.030796e+09  7.417412e+09           4594.98   
12                      1.020574e+09  7.373261e+09           4594.98   

    miners_revenue_btc  miners_revenue_usd  minutes_between_blocks  \
0                 2495         11467926.77                    7.98   
5                 2495         11467926.77                    7.98   
12                2478         11388475.85                    8.01   

    n_blocks_mined  n_blocks_total   n_btc_mined    n_tx  nextretarget  \
0              168          482713  210000000000  273392        483839   
5              168          482713  210000000000  273392        483839   
12             167          482713  208750000000  271638        483839   

     total_btc_sent  total_fees_btc          totalbtc  trade_volume_btc  \
0   164688219250248     39574691936  1653391250000000          44110.58   
5   164688219250248     39574691936  1653391250000000          44110.58   
12  163455939539341     39095614135  1653391250000000          44110.58   

    trade_volume_usd  
0       2.026876e+08  
5       2.026876e+08  
12      2.026876e+08  

I am trying to merge using all_data = pd.merge(btc_price, eth_price, block_data, on = 'time', how = 'outer')however when I do this I get the following error:

我正在尝试使用合并,all_data = pd.merge(btc_price, eth_price, block_data, on = 'time', how = 'outer')但是当我这样做时,我收到以下错误:

File "", line 1, in all_data = pd.merge(btc_price, eth_price, block_data, on = 'time', how = 'outer')

TypeError: merge() got multiple values for argument 'how'

文件 "", line 1, in all_data = pd.merge(btc_price, eth_price, block_data, on = 'time', how = 'outer')

类型错误:merge() 为参数“how”获得了多个值

What does this mean and how can I fix it?

这是什么意思,我该如何解决?

The end result should be one data frame with 22 columns, including all rows from all 3 df. I will then drop the rows with missing values.

最终结果应该是一个包含 22 列的数据框,包括来自所有 3 df 的所有行。然后我将删除缺少值的行。

EDIT: if you look at the timestamps, the first 2 df occur on the minute whereas the third occurs at 03 seconds...is there a way of fixing this? I have a script that pulls these 3 files from json every minute and I am trying to align the 3 df accordingly

编辑:如果您查看时间戳,前 2 个 df 发生在一分钟,而第三个 df 发生在 03 秒......有没有办法解决这个问题?我有一个脚本每分钟从 json 中提取这 3 个文件,我正在尝试相应地对齐 3 df

回答by DYZ

pd.mergecan merge only twoDataFrames. The third parameter (block_datain your case) is interpreted as "how." You also supply the named how='outer', and that's why you see the error message. Solution to your problem: merge the first two DataFrames, then merge the result with the third one.

pd.merge只能合并两个DataFrame。第三个参数(block_data在您的情况下)被解释为“如何”。您还提供了 named how='outer',这就是您看到错误消息的原因。解决您的问题:合并前两个DataFrame,然后将结果与第三个合并。