pandas 当“索引长度不匹配”时,将索引从 DataFrame 复制到第二帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/18391435/
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
Copy index from DataFrame to second frame when 'index length mismatch'
提问by Michal
I have a DataFrame df1(index as a datetime) and df2with many columns,different length index.
I need to combine df1with df2, replacing index in df2. As a result presented df3. 
我有一个 DataFrame df1(索引为日期时间)并且df2有很多列,不同长度的索引。
我需要结合df1使用df2,以替代指标df2。结果出现了df3。
df1
                      T1
2011-09-01 00:00:00   10
2011-09-01 00:10:00   20
2011-09-01 00:20:00   30  
2011-09-01 00:30:00   40
df2
    T2   T3        
0   1.1  2.0 
1   1.2  3.0
2   1.3  4.0
df3
                      T1   T2  T3
2011-09-01 00:00:00   10  1.1  2.0
2011-09-01 00:10:00   20  1.2  3.0
2011-09-01 00:20:00   30  1.3  4.0
2011-09-01 00:30:00   40  Nan  Nan
I wanted to try concat, join, merge, appendbut those doesn't seem to be appropriate.
Using set_indexresulted in having an error: length mismatch.
我想尝试,concat, join, merge, append但那些似乎不合适。
使用set_index导致出现错误:长度不匹配。
I end up trying this:
我最终尝试这样做:
  df3=pd.DataFrame(df2,index=df1.index,copy=True)
I got the desired index, and columns from df2but they were empty.
我得到了所需的索引和列,df2但它们是空的。
回答by Phillip Cloud
Here's one way to do it:
这是一种方法:
In [32]: from pandas import DataFrame, date_range, concat
In [33]: from numpy.random import randn
In [34]: df = DataFrame(randn(5, 1), index=date_range('20010101', periods=5), columns=['A'])
In [35]: df2 = DataFrame(randn(3, 2), columns=list('BC'))
In [36]: concat([df, df2.set_index(df.index[:len(df2)])], axis=1)
Out[36]:
                A      B      C
2001-01-01 -0.043  0.759 -0.125
2001-01-02 -1.377  0.895  0.629
2001-01-03  0.263 -0.007 -0.515
2001-01-04  1.546    NaN    NaN
2001-01-05 -0.657    NaN    NaN
You can also do this with DataFrame.join()for slightly shorter code:
您也可以使用DataFrame.join()稍短的代码执行此操作:
In [7]: df.join(df2.set_index(df.index[:len(df2)]))
Out[7]:
                A      B      C
2001-01-01 -0.607 -0.038  0.593
2001-01-02  0.573  0.399 -0.627
2001-01-03  0.319  0.312 -0.152
2001-01-04 -1.671    NaN    NaN
2001-01-05 -1.589    NaN    NaN
回答by Andy Hayden
Just to throw out another (hacky) method, this one modifies df1:
只是为了抛出另一种(hacky)方法,这个方法修改了df1:
In [11]: df1[df2.columns] = np.nan
In [12]: df1
Out[12]:
                     T1  T2  T3
2011-09-01 00:00:00  10 NaN NaN
           00:10:00  20 NaN NaN
           00:20:00  30 NaN NaN
           00:30:00  40 NaN NaN
In [13]: df1.iloc[:len(df2.index), -len(df2.columns):] = df2.values
In [14]: df1
Out[14]:
                     T1   T2  T3
2011-09-01 00:00:00  10  1.1   2
           00:10:00  20  1.2   3
           00:20:00  30  1.3   4
           00:30:00  40  NaN NaN
Note: This will get tripped up if you have duplicate colnames.
注意:如果您有重复的列名,这会被绊倒。
However, I prefer @PhilipClouds's method.
但是,我更喜欢@PhilipClouds 的方法。

