Pandas 如何提高数据帧的装箱和过滤效率? 问题

Pandas 如何提高数据帧的装箱和过滤效率? 问题,pandas,dataframe,Pandas,Dataframe,我喜欢过滤/减少轨迹数据,只保留每个给定正方形大小的第一次出现 下面我举了一个例子,它可以工作,但是对于我所看到的数据大小来说太慢了(~100_000 id’s和更多的时间步) 所以我的问题是如何有效地做到这一点,也许更短更干净 代码示例 在本例中,过滤器只会删除一行(请参见最后一步,索引“1”)。最后结果的格式也是。。。尴尬 生成虚拟数据 将numpy导入为np 作为pd进口熊猫 def时间(t): 将pd时间戳(“2019-01-01T12”)+pd返回到_timedelta(t,“d”)

我喜欢过滤/减少轨迹数据,只保留每个给定正方形大小的第一次出现

下面我举了一个例子,它可以工作,但是对于我所看到的数据大小来说太慢了(~100_000 id’s和更多的时间步)

所以我的问题是如何有效地做到这一点,也许更短更干净

代码示例 在本例中,过滤器只会删除一行(请参见最后一步,索引“1”)。最后结果的格式也是。。。尴尬

生成虚拟数据
将numpy导入为np
作为pd进口熊猫
def时间(t):
将pd时间戳(“2019-01-01T12”)+pd返回到_timedelta(t,“d”)
数组=[
np.数组([1,1,2,2,3,3]),
np.数组([时间(0)、时间(1)、时间(396)、时间(365)、时间(31)、时间(365)],
]
df=pd.DataFrame(np.array([[1.1,0.9,2.1,3.2,5.5,4.5],[1.2,1.0,2.0,3.0,4.5,5.5]]).T,index=array,columns=[“lon”,“lat”])
df.index.names=[“id”,“time”]
df
返回

        lon     lat
id  time        
1   2019-01-01 12:00:00     1.1     1.2
    2019-01-02 12:00:00     0.9     1.0
2   2020-02-01 12:00:00     2.1     2.0
    2020-01-01 12:00:00     3.2     3.0
3   2019-02-01 12:00:00     5.5     4.5
    2020-01-01 12:00:00     4.5     5.5
        lon     lat     lonbin  latbin
id  time                
1   2019-01-01 12:00:00     1.1     1.2     1.0     1.0
    2019-01-02 12:00:00     0.9     1.0     1.0     1.0
2   2020-02-01 12:00:00     2.1     2.0     2.0     2.0
    2020-01-01 12:00:00     3.2     3.0     3.0     3.0
3   2019-02-01 12:00:00     5.5     4.5     6.0     4.0
    2020-01-01 12:00:00     4.5     5.5     4.0     6.0
    lonbin  latbin  lon     lat
id              
1   1.0     1.0     (1, 2019-01-02 12:00:00)    (1, 2019-01-02 12:00:00)
2   2.0     2.0     (2, 2020-02-01 12:00:00)    (2, 2020-02-01 12:00:00)
2   3.0     3.0     (2, 2020-01-01 12:00:00)    (2, 2020-01-01 12:00:00)
3   4.0     6.0     (3, 2020-01-01 12:00:00)    (3, 2020-01-01 12:00:00)
3   6.0     4.0     (3, 2019-02-01 12:00:00)    (3, 2019-02-01 12:00:00)
备箱
df=df.assign(lonbin=np.round(df.lon,小数=0))
df=df.assign(latbin=np.round(df.lat,小数=0))
df
返回

        lon     lat
id  time        
1   2019-01-01 12:00:00     1.1     1.2
    2019-01-02 12:00:00     0.9     1.0
2   2020-02-01 12:00:00     2.1     2.0
    2020-01-01 12:00:00     3.2     3.0
3   2019-02-01 12:00:00     5.5     4.5
    2020-01-01 12:00:00     4.5     5.5
        lon     lat     lonbin  latbin
id  time                
1   2019-01-01 12:00:00     1.1     1.2     1.0     1.0
    2019-01-02 12:00:00     0.9     1.0     1.0     1.0
2   2020-02-01 12:00:00     2.1     2.0     2.0     2.0
    2020-01-01 12:00:00     3.2     3.0     3.0     3.0
3   2019-02-01 12:00:00     5.5     4.5     6.0     4.0
    2020-01-01 12:00:00     4.5     5.5     4.0     6.0
    lonbin  latbin  lon     lat
id              
1   1.0     1.0     (1, 2019-01-02 12:00:00)    (1, 2019-01-02 12:00:00)
2   2.0     2.0     (2, 2020-02-01 12:00:00)    (2, 2020-02-01 12:00:00)
2   3.0     3.0     (2, 2020-01-01 12:00:00)    (2, 2020-01-01 12:00:00)
3   4.0     6.0     (3, 2020-01-01 12:00:00)    (3, 2020-01-01 12:00:00)
3   6.0     4.0     (3, 2019-02-01 12:00:00)    (3, 2019-02-01 12:00:00)
lat-lon组合首次出现时的过滤
x=pd.DataFrame()
对于集合中的idx(df.index.get_level_值(“id”)):
x=pd.concat(
[
x,,
(
查询(f“id='{idx}')
.groupby([“lonbin”,“latbin”])
.idxmin()
.reset_index()
.assign(id=idx)
.设置索引(“id”)
),
]
)
x
返回

        lon     lat
id  time        
1   2019-01-01 12:00:00     1.1     1.2
    2019-01-02 12:00:00     0.9     1.0
2   2020-02-01 12:00:00     2.1     2.0
    2020-01-01 12:00:00     3.2     3.0
3   2019-02-01 12:00:00     5.5     4.5
    2020-01-01 12:00:00     4.5     5.5
        lon     lat     lonbin  latbin
id  time                
1   2019-01-01 12:00:00     1.1     1.2     1.0     1.0
    2019-01-02 12:00:00     0.9     1.0     1.0     1.0
2   2020-02-01 12:00:00     2.1     2.0     2.0     2.0
    2020-01-01 12:00:00     3.2     3.0     3.0     3.0
3   2019-02-01 12:00:00     5.5     4.5     6.0     4.0
    2020-01-01 12:00:00     4.5     5.5     4.0     6.0
    lonbin  latbin  lon     lat
id              
1   1.0     1.0     (1, 2019-01-02 12:00:00)    (1, 2019-01-02 12:00:00)
2   2.0     2.0     (2, 2020-02-01 12:00:00)    (2, 2020-02-01 12:00:00)
2   3.0     3.0     (2, 2020-01-01 12:00:00)    (2, 2020-01-01 12:00:00)
3   4.0     6.0     (3, 2020-01-01 12:00:00)    (3, 2020-01-01 12:00:00)
3   6.0     4.0     (3, 2019-02-01 12:00:00)    (3, 2019-02-01 12:00:00)

使用(例如)
sns.jointplot(x=x.lonbin,y=x.latbin,kind=“hex”)
我可以查看一个装箱图(或者使用
scipy.stats.kde.gaussian\u kde计算密度).

正如@Ben.T在评论中使用
groupby
和其他两列简化了代码,速度将由
groupby
决定,即

x=(
df.groupby([“id”、“lonbin”、“latbin”])
.idxmin()
.reset_索引(级别=[“lonbin”,“latbin”])
)

您不需要循环和concat,您的输出可以直接通过
x=df.groupby(['id',“lonbin”,“latbin”]).idxmin()获得。reset_index(level=[“lonbin”,“latbin”])
,肯定比在每个循环中使用concat快。重复concat很慢,请在循环结束时使用concat一次:如果我有另一列,对于一些数据,例如
Age
,我如何计算装箱约束中的平均值?