栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

python panda:返回常见行的索引

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python panda:返回常见行的索引

不需要带有连接值的新列,默认情况下,通过两个列进行内部合并,如果需要,则使用

df2.index
add值进行合并
reset_index

df1 = pd.Dataframe({'col1':['cx','cx','cx2'], 'col2':[1,4,12]})df2 = pd.Dataframe({'col1':['cx','cx','cx','cx','cx2','cx2'], 'col2':[1,3,5,10,12,12]})df3 = pd.merge(df1,df2.reset_index(), on = ['col1','col2'])print (df3)  col1 col2  index0   cx    1      01  cx2   12      42  cx2   12      5

对于两个索引都需要:

df4 = pd.merge(df1.reset_index(),df2.reset_index(), on = ['col1','col2'])print (df4)   index_x col1  col2  index_y0        0   cx     1        01        2  cx2    12        42        2  cx2    12        5

仅对于两个Dataframe的交集:

df5 = pd.merge(df1,df2, on = ['col1','col2'])#if 2 column Dataframe   #df5 = pd.merge(df1,df2)print (df5)  col1  col20   cx     11  cx2    122  cx2    12


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号