Python 添加两个数据帧

Python 添加两个数据帧,python,pandas,Python,Pandas,我有两个数据帧,都由timeseries索引。我需要将这些元素添加到一起以形成一个新的数据框架,但前提是索引和列是相同的。如果某个数据帧中不存在该项,则应将其视为零 我尝试过使用.add,但不管索引和列如何,它都会求和。还尝试了一个简单的组合_data=dataframe1+dataframe2,但如果两个数据帧都没有元素,则会给出一个NaN 有什么建议吗?如果我没听错,你想要的是: (x.reindex_like(y).fillna(0) + y.fillna(0).fillna(0)) 这

我有两个数据帧,都由timeseries索引。我需要将这些元素添加到一起以形成一个新的数据框架,但前提是索引和列是相同的。如果某个数据帧中不存在该项,则应将其视为零

我尝试过使用.add,但不管索引和列如何,它都会求和。还尝试了一个简单的组合_data=dataframe1+dataframe2,但如果两个数据帧都没有元素,则会给出一个NaN


有什么建议吗?

如果我没听错,你想要的是:

(x.reindex_like(y).fillna(0) + y.fillna(0).fillna(0))
这将给出两个数据帧的总和。如果一个值在一个数据帧中,而不是在另一个数据帧中,则该位置的结果将是现有值在X中查看B0,在Y中查看B0,并查看最终输出。如果两个数据帧中都缺少一个值,则该位置的结果将为零。查看X中的B1和Y中的B1,然后查看最终输出

>>> x
   A   B   C
0  1   2 NaN
1  3 NaN   4
>>> y
    A   B   C
0   8 NaN  88
1   2 NaN   5
2  10  11  12
>>> (x.reindex_like(y).fillna(0) + y.fillna(0).fillna(0))
    A   B   C
0   9   2  88
1   5   0   9
2  10  11  12
x.addy,fill_value=0怎么样


为了让答案更一般。。。首先,我将获取同步两个数据帧的公共索引,然后将它们连接到我的模式日期,并对相同名称的列求和,最后连接两个数据帧,删除其中一个中添加的列

你可以在这里看到一个谷歌股票价格的例子:

import numpy as np
import pandas as pd
import datetime as dt

prices = pd.DataFrame([[553.0, 555.5, 549.3, 554.11, 0],
                       [556.8, 556.8, 544.05, 545.92, 545.92],
                       [545.5, 546.89, 540.97, 542.04, 542.04]],
                       index=[dt.datetime(2014,11,04), dt.datetime(2014,11,05), dt.datetime(2014,11,06)],
                       columns=['Open', 'High', 'Low', 'Close', 'Adj Close'])

corrections = pd.DataFrame([[0, 555.22], [1238900, 0]],
                    index=[dt.datetime(2014,11,3), dt.datetime(2014,11,4)],
                    columns=['Volume', 'Adj Close'])

dates = pd.DataFrame(prices.index, columns = ['Dates']).append(pd.DataFrame(corrections.index, columns = ['Dates'])).drop_duplicates('Dates').set_index('Dates').sort(axis=0)
df_corrections = dates.join(corrections).fillna(0)
df_prices = dates.join(prices).fillna(0)

for col in prices.columns:
    if col in corrections.columns:
        df_prices[col]+=df_corrections[col]
        del df_corrections[col]

df_prices = df_prices.join(df_corrections)
如果以上两个答案中的任何一个有不同的结构,那么fillna0和直接加法都会给出Nan值

最好使用fill_值


df.addother_df,fill_value=0

如果一个或两个数据框中都不存在某个项,您能否澄清您希望发生什么?如果一个数据帧中不存在该项,则应将其视为零您是说该数据帧中的值应视为零并添加到另一个数据帧中的值,还是说结果数据帧中的值应为零?另外,您说df1+df2不起作用,因为如果两者都没有元素,它会给出NaN。你想在这种情况下发生什么?你想要结果为零?很好,正是我想要的。ThanksI最近发现,如果您首先创建两个数据帧,然后使用df将其索引列设置为现有数据帧列之一,则此方法不起作用。例如,设置_index'Column_A',谢谢,但我没有很好地解释我的数据,因为我在两个数据帧中都有不同的列,例如,dataframe1中的A、B、C和A、B,数据帧2中的D。输出应该是带有a、B、C、D的数据帧
import numpy as np
import pandas as pd
import datetime as dt

prices = pd.DataFrame([[553.0, 555.5, 549.3, 554.11, 0],
                       [556.8, 556.8, 544.05, 545.92, 545.92],
                       [545.5, 546.89, 540.97, 542.04, 542.04]],
                       index=[dt.datetime(2014,11,04), dt.datetime(2014,11,05), dt.datetime(2014,11,06)],
                       columns=['Open', 'High', 'Low', 'Close', 'Adj Close'])

corrections = pd.DataFrame([[0, 555.22], [1238900, 0]],
                    index=[dt.datetime(2014,11,3), dt.datetime(2014,11,4)],
                    columns=['Volume', 'Adj Close'])

dates = pd.DataFrame(prices.index, columns = ['Dates']).append(pd.DataFrame(corrections.index, columns = ['Dates'])).drop_duplicates('Dates').set_index('Dates').sort(axis=0)
df_corrections = dates.join(corrections).fillna(0)
df_prices = dates.join(prices).fillna(0)

for col in prices.columns:
    if col in corrections.columns:
        df_prices[col]+=df_corrections[col]
        del df_corrections[col]

df_prices = df_prices.join(df_corrections)