【发布时间】:2016-01-27 01:58:52
【问题描述】:
我有两张桌子。一个(下面的 df)大约有 18,000 行,另一个(下面的地图文件)有大约 800,000 行。我需要一个可以处理如此大的 DataFrame 的解决方案。
这是一个玩具示例: 表 1 - df
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
表 2 - 映射文件
Name Chr Position
P1 1 105
P2 1 60
P3 1 500
P4 2 25
P5 2 220
P6 2 240
我正在尝试执行以下操作(我的语法错误,但我认为这个想法出现了):
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'] ] )
言下之意: 我需要遍历 mapfile 中的每个项目(行),看看它的位置是否在 df 中每个 CHR 上的任何 START 和 END 之间。如果是,我需要将其添加到包含两个表中的 Name、Chr、Position、Sample 和 Value 字段的新文件中。
玩具数据输出表:
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
到目前为止: 我已经有了上面的内容,并且一直在找出语法以在 python 中执行一般循环时遇到问题。但是,我的理解是,使用 pandas 或 NumPy 之类的包可能会更容易?请帮助我找到最有效的方法来执行此操作,并且在此过程中对语法提供一些帮助会很棒。
我尝试过的一些相关帖子但无法正常工作 What is the most efficient way to loop through dataframes with pandas? How to iterate over rows in a DataFrame in Pandas? Append column to pandas dataframe Conditionally fill column values based on another columns value in pandas
【问题讨论】:
-
您能添加代码来生成玩具示例吗?另外,您能否列出该示例的最终输出?
-
@Divakar ,我不知道如何编写代码以在 python 中生成示例。对不起。但我会在我到达计算机后立即添加最终输出。