繁体   English   中英

熊猫合并/合并数据框

[英]Pandas Merging/Join Dataframes

我不确定这意味着合并还是加入。

我有一个带有以下列['times','spots'] ,DataFrame A的数据框,我还有另一个具有相似的列['times','spots'] ,DataFrame B的数据框。

我想更改数据框A,以便它具有从数据框B插入的值,而这又是从数据框A插入的。因此,它将在数据框A中有一个新列Spots_B。

好吧,我将在壁架上向您展示如何与后缀合并:

# First import our libraries
>>> import pandas as pd
>>> import numpy as np
# Then create our dataframes
>>> df_A = pd.DataFrame(np.random.rand(3,2),columns=['times','spots'])
>>> df_B = pd.DataFrame(np.random.rand(3,2),columns=['times','spots'])
# Set default values
>>> df_A['times'] = [1,2,3]
>>> df_B['times'] = [1,2,3]
>>> df_A['spots'] = [44,55,66]
>>> df_B['spots'] = [77,88,99]
# Here is what both dataframes contain
>>> df_A
   times  spots
0      1     44
1      2     55
2      3     66
>>> df_B
   times  spots
0      1     77
1      2     88
2      3     99
# Now the merge -- note: this does not affect the first dataframe in place.
#                        It will create a new dataframe. You can overwrite the 
#                        first if you set the result to df_A instead of df_merged.
# Note the use of the keyword, suffixes. In the event that the same column names exist
#  in both dataframes (that aren't being merged on) Pandas will need to differentiate
#  between them. By default same column names will result in a '_x' will be appended to
#  the left dataframe column name, and a '_y' to the right dataframe column name
#  [order is set by the first two arguments in the merge function]. 
#  The suffixes keyword allows the user to override this behaviour with their
#  own version of '_x' and '_y'.
>>> df_merged = pd.merge(df_A,df_B,how='inner',on=['times'],suffixes=['_A','_B'])
>>> df_merged
   times  spots_A  spots_B
0      1       44       77
1      2       55       88
2      3       66       99

现在从您的问题看来,您不想修改斑点的第一个数据框列名称。 这可以以相同的方式来实现,只是不使用suffixes=['_A','_B']使用suffixes=['','_B'] 这实际上将左边的dataframe列后缀设置为空,因此保持不变:

>>> df_merged = pd.merge(df_A,df_B,how='inner',on=['times'],suffixes=['','_B'])
>>> df_merged
   times  spots  spots_B
0      1     44       77
1      2     55       88
2      3     66       99

瞧! 希望对您有所帮助。 如果我误解了,而您实际上正在寻找A和B之间的插值,请告诉我,我将编辑此答案。

*编辑1 *

考虑到您的最后评论,这是我认为您正在努力实现的目标。 下面,我将向您展示如何扩展带后缀的合并,然后使用“时间”插值方法用插值填充Spots_B中的NaN

# Start by creating out datetimes to set for the times column
>>> times_A = []
>>> times_B = []
>>> for i in range(1,4):
...   times_A.append(datetime.datetime(year=2011,month=5,day=i))
...
>>> for i in range(1,6,2):
...   times_B.append(datetime.datetime(year=2011,month=5,day=i))
...
# times_A: May 1st, 2011 - May 3rd, 2011
>>> times_A
[datetime.datetime(2011, 5, 1, 0, 0), datetime.datetime(2011, 5, 2, 0, 0), datetime.datetime(2011, 5, 3, 0, 0)]
# times_B: May 1st 2011, May 3rd 2011, May 5th 2011
>>> times_B
[datetime.datetime(2011, 5, 1, 0, 0), datetime.datetime(2011, 5, 3, 0, 0), datetime.datetime(2011, 5, 5, 0, 0)]
# So now times_B is missing May 2nd, and has an extra time, May 5th.
>>> df_A['times'] = times_A
>>> df_B['times'] = times_B
>>> df_A['spots'] = [44,55,66]
>>> df_B['spots'] = [44,66,88]
>>> df_A
                times  spots
0 2011-05-01 00:00:00     44
1 2011-05-02 00:00:00     55
2 2011-05-03 00:00:00     66
>>> df_B
                times  spots
0 2011-05-01 00:00:00     44
1 2011-05-03 00:00:00     66
2 2011-05-05 00:00:00     88

# Now it appears you only care about the times in df_A - so
#   left merge df_A with df_B (include all times from df_A and  
#   try to merge with df_B or NaN). Below the date May 5th was dropped.
>>> df_merged = pd.merge(df_A,df_B,how='left',on=['times'],suffixes=['','_B'])
>>> df_merged
                times  spots  spots_B
0 2011-05-01 00:00:00     44       44
1 2011-05-02 00:00:00     55      NaN
2 2011-05-03 00:00:00     66       66

# Here is the important part:
# Since it appears that your data is going to be a time series
#   you will need to set your dataframe index to be the times column.
>>> df_merged = df_merged.set_index(['times'])
>>> df_merged
            spots  spots_B
times
2011-05-01     44       44
2011-05-02     55      NaN
2011-05-03     66       66

# With the times as index we can use the appropriate
#   interpolation method for best results
>>> df_merged['spots_B'] = df_merged['spots_B'].interpolate(method='time')
>>> df_merged
            spots  spots_B
times
2011-05-01     44       44
2011-05-02     55       55
2011-05-03     66       66

注意: Seriesinterpolate()的默认行为是假设每一行都是等距的。 如果时间间隔不相等,则需要使用TimeSeries索引重新索引数据帧。 如果索引是时间序列,则可以在interpolate()函数中使用method='time'参数。

注意2:提出问题时,请尝试提供尽可能多的细节。 它可以帮助想要回答的人完全了解您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM