Python 根据布尔向量组合2个数据帧
我的问题如下:Python 根据布尔向量组合2个数据帧,python,pandas,Python,Pandas,我的问题如下: 假设我在pandas中有两个列数相同的数据帧,例如: A= 1 2 3 4 8 9 及 还有一个布尔向量,其长度正好是A+num of B rows=5中的num of rows,其中1s的数量与B中的num of rows相同,在本例中,这意味着两个1s。 比如说Bool=0110 然后,我的目标是将A和B合并成一个更大的数据帧,称为C,这样B的行对应于Bool中的1,因此在这个示例中,它将给出: C= 1 2 7 8 3 4 4 0
假设我在pandas中有两个列数相同的数据帧,例如:
A= 1 2
3 4
8 9
及
还有一个布尔向量,其长度正好是A+num of B rows=5中的num of rows,其中1
s的数量与B中的num of rows相同,在本例中,这意味着两个1
s。
比如说Bool=0110
然后,我的目标是将A和B合并成一个更大的数据帧,称为C,这样B的行对应于Bool中的1,因此在这个示例中,它将给出:
C= 1 2
7 8
3 4
4 0
8 9
你知道怎么做吗?
如果你知道这对我有多大帮助的话。
感谢阅读。一个选项是创建一个具有预期形状的空数据框,然后将A和B中的值填入:
这里有一个pandas唯一的解决方案,可以重新索引原始数据帧,然后将其连接起来:
Bool = pd.Series([0, 1, 0, 1, 0], dtype=bool)
B.index = Bool[ Bool].index
A.index = Bool[~Bool].index
pd.concat([A,B]).sort_index() # sort_index() is not really necessary
# 0 1
#0 1 2
#1 7 8
#2 3 4
#3 4 0
#4 8 9
以下方法将推广到比2更大的组。从
A = pd.DataFrame([[1,2],[3,4],[8,9]])
B = pd.DataFrame([[7,8],[4,0]])
C = pd.DataFrame([[9,9],[5,5]])
bb = pd.Series([0, 1, 0, 1, 2, 2, 0])
我们可以使用
pd.concat([A, B, C]).iloc[bb.rank(method='first')-1].reset_index(drop=True)
给
In [269]: pd.concat([A, B, C]).iloc[bb.rank(method='first')-1].reset_index(drop=True)
Out[269]:
0 1
0 1 2
1 7 8
2 3 4
3 4 0
4 9 9
5 5 5
6 8 9
这是因为当您使用
method='first'
时,它会按值的顺序对值进行排序,然后再按它们的显示顺序进行排序。这意味着我们得到了
In [270]: pd.Series([1, 0, 0, 1, 0]).rank(method='first')
Out[270]:
0 4.0
1 1.0
2 2.0
3 5.0
4 3.0
dtype: float64
这正是(减去一)我们想要选择行的iloc顺序。这太聪明了!明亮的你们两个!因为你的答案倾向于更一般化,所以我会考虑使用<代码> Atype(A.DyType)<代码>某处。你们两个!你在一篇现已删除的帖子中提到,这些答案对你有所帮助。请将与您的问题最相关的一个标记为已接受。这正是网站得以流传的原因。
In [269]: pd.concat([A, B, C]).iloc[bb.rank(method='first')-1].reset_index(drop=True)
Out[269]:
0 1
0 1 2
1 7 8
2 3 4
3 4 0
4 9 9
5 5 5
6 8 9
In [270]: pd.Series([1, 0, 0, 1, 0]).rank(method='first')
Out[270]:
0 4.0
1 1.0
2 2.0
3 5.0
4 3.0
dtype: float64