大佬教程收集整理的这篇文章主要介绍了Python Pandas-比较2个数据框,多个参数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
read_csv
和@H_86_3@merge:
import pandas as pd
import io
temp1=u"""Sample;Chr;Start;End;Value
S1;1;100;200;1
S1;2;200;250;1
S2;1;50;75;5
S2;2;150;225;4"""
#after tesTing replace io.StringIO(temp1) to filename
dfline = pd.read_csv(io.StringIO(temp1), sep=";")
temp2=u"""name;Chr;position
P1;1;105
P2;1;60
P3;1;500
P4;2;25
P5;2;220
P6;2;240"""
#after tesTing replace io.StringIO(temp2) to filename
mapfile = pd.read_csv(io.StringIO(temp2), sep=";")
print dfline
Sample Chr Start End Value
0 S1 1 100 200 1
1 S1 2 200 250 1
2 S2 1 50 75 5
3 S2 2 150 225 4
print mapfile
name Chr position
0 P1 1 105
1 P2 1 60
2 P3 1 500
3 P4 2 25
4 P5 2 220
5 P6 2 240
#merge by column Chr
df = pd.merge(dfline, mapfile, on=['Chr'])
#SELEct by conditions
df = df[(df.position > df.Start) & (df.position < df.End)]
#subset of df
df = df[['name','Chr','position','Value', 'Sample']]
print df
name Chr position Value Sample
0 P1 1 105 1 S1
4 P2 1 60 5 S2
7 P5 2 220 1 S1
8 P6 2 240 1 S1
10 P5 2 220 4 S2
#if you need reset index
print df.reset_index(drop=TruE)
name Chr position Value Sample
0 P1 1 105 1 S1
1 P2 1 60 5 S2
2 P5 2 220 1 S1
3 P6 2 240 1 S1
4 P5 2 220 4 S2
Sample Chr Start End Value
S1 1 100 200 1
S1 2 200 250 1
S2 1 50 75 5
S2 2 150 225 4
@H_944_2@表2-映射文件
Name Chr Position
P1 1 105
P2 1 60
P3 1 500
P4 2 25
P5 2 220
P6 2 240
@H_944_2@我正在尝试执行以下操作(我的语法是错误的,但是我认为这个想法会出现):
for mapline in mapfile:
for dfline in df:
if Df[dfline]['Chr'] == mapfile[mapline]['Chr']
if mapfile[mapline]['Position'] > df[dfline]['Start'] & mapfile[mapline]['Position'] < df[dfline]['End']
newdf[['Name','Chr','Position','Value','Sample']] = pd.DataFrame([ mapfile[mapline]['Name'],mapfile[mapline]['Chr'],mapfile[mapline]['Position'],df[dfline]['Value'],df[dfline]['Sample'] ] )
@H_944_2@换句话说:我需要遍历mapfile中的每个项目(行),看看它的位置是否在df中每个CHR的任何START和END之间。如果是这样,我需要使用两个表中的“名称”,“色度”,“位置”,“样本”和“值”字段将其添加到新文件中。
@H_944_2@玩具数据输出表:
Name Chr Position Value Sample
P1 1 105 1 S1
P2 1 60 5 S2
P5 2 220 1 S1
P5 2 220 4 S2
P6 2 240 1 S1
@H_944_2@到目前为止:我已经了解了上面的内容,并且在确定语法以在python中进行常规循环时遇到了问题。但是,我的理解是,使用pandas或NumPy这样的包可能会容易得多?请帮助我找到最有效的方法,此过程中对语法的一些帮助将非常有用。
以上是大佬教程为你收集整理的Python Pandas-比较2个数据框,多个参数全部内容,希望文章能够帮你解决Python Pandas-比较2个数据框,多个参数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。