我对pandas不熟悉,我正在尝试绘制多个列,而不是仅绘制一个。This page向我展示了如何使用pd.Series
来实现这一点,但我无法理解如何映射多个columns
。在
这是我的两个DataFrames
我正试着map
。在
data2=pd.DataFrame(np.random.randn(5,2),index=range(0,5),columns=['x','y'])
data2['Cluster']=['A','B','A','B','C']
centers2=pd.DataFrame(np.random.randint(0,10,size=(3,2)),index= ['A','B','C'],columns=['x','y'])
这里data2
看起来像:
并且centers2
看起来像:
centers2
x y
A 6 4
B 6 0
C 4 1
我希望在data2
中创建两个独立的列,并使用适当的center2
匹配。这是我的手动尝试
data2['Centers.x']=[6,6,6,6,4]
data2['Centers.y']=[4,0,4,0,1]
data2
x y Cluster Centers.x Centers.y
0 0.151212 -0.168855 A 6 4
1 -0.078935 1.933378 B 6 0
2 -0.388903 0.444610 A 6 4
3 0.622089 1.609730 B 6 0
4 -0.346856 1.095834 C 4 1
如何使用map
函数来实现这一点?(我知道如何使用循环来实现这一点,我需要一个矢量化的解决方案。)
您可以将^{} 与^{} 一起使用:
带^{} 的溶液:docs
^{pr2}$另一个带有^{} 的解决方案与
join
相同,但是添加了2
参数:计时:
len(df)=5k
:.merge()
与pd.Series.map()
最接近。可以使用suffixes=[]
关键字向重叠列添加自定义标头,例如suffices=['', '_centers']
。在注意}没有{}。在
pd.Series
没有.merge()
,而{与
你会得到:
^{pr2}$还有一个}如果{}对两个{}都是打开的}:
.join()
选项,它是另一种访问.merge()
的方法,或者{index
,则从源代码访问{相关问题 更多 >
编程相关推荐