【问题标题】:Inserting Series into DataFrame with automatic reindexing使用自动重新索引将 Series 插入 DataFrame
【发布时间】:2014-09-11 15:45:11
【问题描述】:

我有一个 DataFrame 和一系列不同的维度

from pandas import *
df = DataFrame({'a':[1,2,3],'b':[1,2,3]})
s = Series([1,2,3,4,5])

有没有办法将s 插入df 而无需先创建df 的重新索引副本?目前我正在使用

df = df.reindex(range(len(s))
df['s'] = s

print df
    a   b  s
0   1   1  1
1   2   2  2
2   3   3  3
3 NaN NaN  4
4 NaN NaN  5

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用concat:

    In [19]:
    
    concat([df,s], axis=1)
    
    Out[19]:
        0   1  2
    0   1   1  1
    1   2   2  2
    2   3   3  3
    3 NaN NaN  4
    4 NaN NaN  5
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2016-02-28
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2017-08-07
      相关资源
      最近更新 更多