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

Python Pandas-比较2个数据框,多个参数

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

Python Pandas-比较2个数据框,多个参数

您可以使用IIUC

read_csv
merge

import pandas as pdimport iotemp1=u"""Sample;Chr;Start;End;ValueS1;1;100;200;1S1;2;200;250;1S2;1;50;75;5S2;2;150;225;4"""#after testing replace io.StringIO(temp1) to filenamedfline = pd.read_csv(io.StringIO(temp1), sep=";")temp2=u"""Name;Chr;PositionP1;1;105P2;1;60P3;1;500P4;2;25P5;2;220P6;2;240"""#after testing replace io.StringIO(temp2) to filenamemapfile = pd.read_csv(io.StringIO(temp2), sep=";")print dfline  Sample  Chr  Start  End  Value0     S1    1    100  200      11     S1    2    200  250      12     S2    1     50   75      53     S2    2    150  225      4print mapfile  Name  Chr  Position0   P1    1       1051   P2    1        602   P3    1       5003   P4    2        254   P5    2       2205   P6    2       240#merge by column Chrdf = pd.merge(dfline, mapfile, on=['Chr'])#select by conditionsdf = df[(df.Position > df.Start) & (df.Position < df.End)]#subset of dfdf =  df[['Name','Chr','Position','Value', 'Sample']]print df   Name  Chr  Position  Value Sample0    P1    1       105      1     S14    P2    1        60      5     S27    P5    2       220      1     S18    P6    2       240      1     S110   P5    2       220      4     S2#if you need reset indexprint df.reset_index(drop=True)  Name  Chr  Position  Value Sample0   P1    1       105      1     S11   P2    1        60      5     S22   P5    2       220      1     S13   P6    2       240      1     S14   P5    2       220      4     S2


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

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

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