Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 添加两个索引不同的数据帧_Python_Pandas_Series - Fatal编程技术网

Python 添加两个索引不同的数据帧

Python 添加两个索引不同的数据帧,python,pandas,series,Python,Pandas,Series,我有两个数据帧,每一个都有很多列和行。每行中的元素相同,但它们的索引不同。我想添加两个数据帧的其中一列的元素 作为一个基本的例子,考虑以下两个系列: Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3]) Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4]) 假设每一行包含相同的元素,只是在不同的索引中。我想添加这两列,最后得到一个新列,其中包含[4,6,0,10]。相反,由于索引,我得到了[nan,5,7

我有两个数据帧,每一个都有很多列和行。每行中的元素相同,但它们的索引不同。我想添加两个数据帧的其中一列的元素

作为一个基本的例子,考虑以下两个系列:

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])
假设每一行包含相同的元素,只是在不同的索引中。我想添加这两列,最后得到一个新列,其中包含
[4,6,0,10]
。相反,由于索引,我得到了
[nan,5,7,1]

有没有一种简单的方法可以在不改变指数的情况下解决这个问题

我希望输出为一个系列。

使用
zip

Ex:

import pandas as pd

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

sr3 = [sum(i) for i in zip(Sr1, Sr2)]
print(sr3)
[4, 6, 0, 10]
输出:

import pandas as pd

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

sr3 = [sum(i) for i in zip(Sr1, Sr2)]
print(sr3)
[4, 6, 0, 10]
使用
zip

Ex:

import pandas as pd

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

sr3 = [sum(i) for i in zip(Sr1, Sr2)]
print(sr3)
[4, 6, 0, 10]
输出:

import pandas as pd

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

sr3 = [sum(i) for i in zip(Sr1, Sr2)]
print(sr3)
[4, 6, 0, 10]
您可以使用,这将为您提供一个numpy表示,然后您可以这样添加它们:

Sr1.values + Sr2.values
您可以使用,这将为您提供一个numpy表示,然后您可以这样添加它们:

Sr1.values + Sr2.values

一种方法是在一个或多个系列上使用
reset_index

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

res = Sr1 + Sr2.reset_index(drop=True)

0     4
1     6
2     0
3    10
dtype: int64

一种方法是在一个或多个系列上使用
reset_index

Sr1 = pd.Series([1,2,3,4], index = [0, 1, 2, 3])
Sr2 = pd.Series([3,4,-3,6], index = [1, 2, 3, 4])

res = Sr1 + Sr2.reset_index(drop=True)

0     4
1     6
2     0
3    10
dtype: int64

您可以使用重置索引(drop=True):

而且


您可以使用重置索引(drop=True):

而且


预期输出的格式是什么?列表还是系列?如果您不想担心索引对齐,我建议您使用numpy数组。您好,它应该是系列。那么输出系列应该有哪些索引?应将此信息添加到问题中。是否在“帮助”下面找到答案?请随意接受答案(左侧绿色勾选),或要求澄清。预期输出的格式是什么?列表还是系列?如果您不想担心索引对齐,我建议您使用numpy数组。您好,它应该是系列。那么输出系列应该有哪些索引?应将此信息添加到问题中。是否在“帮助”下面找到答案?欢迎接受答案(左边绿色勾选),或要求澄清。您好,谢谢您的建议。拉链很整齐。我不知道。我希望结果会是一个系列,正如我上面所说的。当涉及熊猫和numpy时,迭代是不必要的,并且不能很好地扩展。嗨,谢谢你的建议。拉链很整齐。我不知道。我希望结果会是一个系列,正如我上面所说的。当涉及熊猫和numpy时,迭代是不必要的,并且不能很好地扩展。对,这个
reset\u index
不会干扰原始系列的索引,但只能临时使用。感谢您,此
reset_index
不会影响原始系列的索引,但只能临时使用。谢谢