我有不同 pandas 系列的结果,最终形成 1x1 系列。现在我想将它们全部合并到 1xN 行。他们每个人都可以有不同的索引。
我可以为它们重置索引并创建一个新的数据框。但是,我想知道是否有比这更快/更简单的方法。
edit1:添加数据和所需的输出示例
A
326 0.005077
dtype: float64
date
4300 2011-01-18 16:00:00
Name: datetime, dtype: datetime64[ns]
B
5 0.004077
dtype: float64
我想要这样的东西:
Index A B
2011-01-18 16:00:00 0.005077 0.004077
我想要这个的原因:程序运行一个循环并返回上述 1xN 行的多个,如果日期不同,我想将这些行相互追加,或者如果日期(索引)相同,则将这些值相加(可以如下所示完成:Loop: Results to be updated via += if on the same date, otherwise write next line
edit2:抱歉,我的代码中有一些异常(exception),我必须设置某些系列对象= 0。有没有什么方法可以将其包含在下面的一行中,或者我是否需要将 pd.series 排除在零之外?
最佳答案
我认为需要:
A = pd.Series([0.005077], index=[326])
date = pd.Series(['2011-01-18 16:00:00'], index=[4300])
B = pd.Series([ 0.004077], index=[5])
df = pd.DataFrame([np.concatenate([A, B])], columns=['A','B'], index=date)
print (df)
A B
2011-01-18 16:00:00 0.005077 0.004077
编辑:
对于标量,有必要创建一个项目列表:
A = pd.Series([0.005077], index=[326])
date = pd.Series(['2011-01-18 16:00:00'], index=[4300])
B = pd.Series([ 0.004077], index=[5])
C = 0
df = pd.DataFrame([np.concatenate([A, B, [C] ])], columns=['A','B','C'], index=date)
print (df)
A B C
2011-01-18 16:00:00 0.005077 0.004077 0.0
关于python - 合并具有不同索引的 pandas 系列(在一个索引上对齐),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49704687/