相交2系列
intersect 2 series
我有 2 个这样的系列。我将其称为 s1 和 s2
1 a
1 b
1 c
2 a
2 c
3 b
s2:
1 a
1 b
2 c
3 a
如何合并 2 个系列,只需保持行具有相同的索引和值并将其保存到一个新系列中?我正在寻找如何使用 combine 但它似乎不起作用。我想要结果是系列,因为我想在上面使用 value_counts
例如:我想组合出现在 2 个系列中的 Windowns
具有相同的索引,例如 s1 中的 1a 和 s2 中的 1a,并且 [index,value] 将被添加到 s3(结果),如果 Windowns 不在 s1 的索引 1a 中或者s2,不会加到s3
s3:
1 a
1 b
2 c
谢谢
使用GroupBy.size
for counts in original Series
, then filter same indices in both by Series.loc
with Index.intersection
and last count both with Series.add
:
s11 = s1.groupby([s1.index, s1]).size()
s22 = s2.groupby([s2.index, s2]).size()
idx = s11.index.intersection(s22.index)
df = s11.loc[idx].add(s22.loc[idx]).rename_axis(('idx','vals')).reset_index(name='count')
print (df)
idx vals count
0 1 Windows 2
1 65112 Arduino 2
2 65112 Linux 2
3 65112 Raspberry Pi 2
4 65112 Windows 2
编辑:仍在等待评论,但如果 s11
和 s22
中不仅有 1
值,请使用:
s11 = s1.groupby([s1.index, s1]).size()
s22 = s2.groupby([s2.index, s2]).size()
idx = s11.index.intersection(s22.index)
s3 = pd.Series(idx.get_level_values(1), idx.get_level_values(0))
print (s3)
1 Windows
65112 Arduino
65112 Linux
65112 Raspberry Pi
65112 Windows
dtype: object
如果 s11
和 s22
中的值始终为 1
,则表示每个索引使用的值都是唯一的:
s11 = s1.to_frame().set_index('PlatformWorkedWith', append=True)
s22 = s2.to_frame().set_index('PlatformDesireNextYear', append=True)
idx = s11.index.intersection(s22.index)
s3 = pd.Series(idx.get_level_values(1), idx.get_level_values(0))
print (s3)
1 Windows
65112 Arduino
65112 Linux
65112 Raspberry Pi
65112 Windows
dtype: object
我有 2 个这样的系列。我将其称为 s1 和 s2
1 a
1 b
1 c
2 a
2 c
3 b
s2:
1 a
1 b
2 c
3 a
如何合并 2 个系列,只需保持行具有相同的索引和值并将其保存到一个新系列中?我正在寻找如何使用 combine 但它似乎不起作用。我想要结果是系列,因为我想在上面使用 value_counts
例如:我想组合出现在 2 个系列中的 Windowns
具有相同的索引,例如 s1 中的 1a 和 s2 中的 1a,并且 [index,value] 将被添加到 s3(结果),如果 Windowns 不在 s1 的索引 1a 中或者s2,不会加到s3
s3:
1 a
1 b
2 c
谢谢
使用GroupBy.size
for counts in original Series
, then filter same indices in both by Series.loc
with Index.intersection
and last count both with Series.add
:
s11 = s1.groupby([s1.index, s1]).size()
s22 = s2.groupby([s2.index, s2]).size()
idx = s11.index.intersection(s22.index)
df = s11.loc[idx].add(s22.loc[idx]).rename_axis(('idx','vals')).reset_index(name='count')
print (df)
idx vals count
0 1 Windows 2
1 65112 Arduino 2
2 65112 Linux 2
3 65112 Raspberry Pi 2
4 65112 Windows 2
编辑:仍在等待评论,但如果 s11
和 s22
中不仅有 1
值,请使用:
s11 = s1.groupby([s1.index, s1]).size()
s22 = s2.groupby([s2.index, s2]).size()
idx = s11.index.intersection(s22.index)
s3 = pd.Series(idx.get_level_values(1), idx.get_level_values(0))
print (s3)
1 Windows
65112 Arduino
65112 Linux
65112 Raspberry Pi
65112 Windows
dtype: object
如果 s11
和 s22
中的值始终为 1
,则表示每个索引使用的值都是唯一的:
s11 = s1.to_frame().set_index('PlatformWorkedWith', append=True)
s22 = s2.to_frame().set_index('PlatformDesireNextYear', append=True)
idx = s11.index.intersection(s22.index)
s3 = pd.Series(idx.get_level_values(1), idx.get_level_values(0))
print (s3)
1 Windows
65112 Arduino
65112 Linux
65112 Raspberry Pi
65112 Windows
dtype: object