Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 - Fatal编程技术网

Python 根据值对齐两个系列

Python 根据值对齐两个系列,python,pandas,Python,Pandas,我试图将数据分为两个系列,并确定每个系列中的漏洞。我有一个解决方案,我想看看是否有更好的方法来做到这一点 范例 Series 1 Series 2 A B B C D D Output A B B C D D 我的解决方案 import pandas as pd import n

我试图将数据分为两个系列,并确定每个系列中的漏洞。我有一个解决方案,我想看看是否有更好的方法来做到这一点

范例

Series 1                  Series 2
A                         B
B                         C
D                         D

Output
A     
B     B
      C
D     D
我的解决方案

import pandas as pd
import numpy as np
x = pd.Series( np.arange(3), index=['A', 'B', 'D'] )
y = pd.Series( np.arange(3), index=['B', 'C', 'D'] )
Z = pd.concat([x,y], axis=1)                            # Align by index
Z1 = Z[0].reset_index().rename({'index': 'x'}, axis=1)
Z1.loc[Z1[0].isna(), 'x'] = ''
Z2 = Z[1].reset_index().rename({'index': 'y'}, axis=1)
Z2.loc[Z2[1].isna(), 'y'] = ''
pd.concat([ Z1['x'], Z2['y'] ], axis=1)
输出

Out[67]:
   x  y
0  A
1  B  B
2     C
3  D  D

由于这些系列的索引中有A、B、C、D,我更愿意返回它们,而不是将它们作为值:

In [11]: pd.DataFrame.from_dict({"x": x, "y": y})
Out[11]:
     x    y
A  0.0  NaN
B  1.0  0.0
C  NaN  1.0
D  2.0  2.0

In [12]: pd.DataFrame.from_dict({"x": x, "y": y}).isnull()
Out[12]:
       x      y
A  False   True
B  False  False
C   True  False
D  False  False

由于这些系列的索引中有A、B、C、D,我更愿意返回它们,而不是将它们作为值:

In [11]: pd.DataFrame.from_dict({"x": x, "y": y})
Out[11]:
     x    y
A  0.0  NaN
B  1.0  0.0
C  NaN  1.0
D  2.0  2.0

In [12]: pd.DataFrame.from_dict({"x": x, "y": y}).isnull()
Out[12]:
       x      y
A  False   True
B  False  False
C   True  False
D  False  False
与in的理解 理解与对齐 与in的理解 理解与对齐 让我们使用pd.factorize和union索引,然后使用pd.concat新构造的pd.Series和使用map生成的索引:

输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
或者我们可以使用pd.Index.to_系列,而不是pd.series构造函数:

mapper = dict(zip(*pd.factorize(x.index.union(y.index))[::-1]))

pd.concat([x.index.to_series(x.index.map(mapper), name='x'), 
           y.index.to_series(y.index.map(mapper), name='y')], axis=1).fillna('')
输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
我喜欢@piRSquared使用align with pd.Index.to_系列 使用@piRSquared idea for align,我们可以将其制作成整洁的一行:

pd.concat(x.index.to_series(name='x').align(y.index.to_series(name='y')), axis=1)\
  .reset_index(drop=True).fillna('')
输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
让我们使用pd.factorize和union索引,然后使用pd.concat新构造的pd.Series和使用map生成的索引:

输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
或者我们可以使用pd.Index.to_系列,而不是pd.series构造函数:

mapper = dict(zip(*pd.factorize(x.index.union(y.index))[::-1]))

pd.concat([x.index.to_series(x.index.map(mapper), name='x'), 
           y.index.to_series(y.index.map(mapper), name='y')], axis=1).fillna('')
输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
我喜欢@piRSquared使用align with pd.Index.to_系列 使用@piRSquared idea for align,我们可以将其制作成整洁的一行:

pd.concat(x.index.to_series(name='x').align(y.index.to_series(name='y')), axis=1)\
  .reset_index(drop=True).fillna('')
输出:

   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D
   x  y
0  A   
1  B  B
2     C
3  D  D

要复制OPs输出:pd.DataFrame.from_dict{x:x,y:y}.notna.pipelambda d:d.muld.index,axis=0.reset_indexdrop=true,我喜欢布尔值与索引相乘的想法。对于其他的“``A'*True->`A'*False->``复制OPs输出:pd.DataFrame.from_dict{x:x,y:y}.notna.pipelambda d:d.muld.index,axis=0.reset_indexdrop=True我喜欢将布尔值与索引相乘的想法。对于其他人来说,``` A'*True->` A'*False->``我喜欢你的对齐方式。我偷了它。谢谢我喜欢你的工作。我偷了它。谢谢