在由索引分隔的部分中插入

在由索引分隔的部分中插入

本文介绍了在由索引分隔的部分中插入 Pandas Dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的示例代码如下:

import pandas as pd


dictx = {'col1':[1,'nan','nan','nan',5,'nan',7,'nan',9,'nan','nan','nan',13],\
    'col2':[20,'nan','nan','nan',22,'nan',25,'nan',30,'nan','nan','nan',25],\
    'col3':[15,'nan','nan','nan',10,'nan',14,'nan',13,'nan','nan','nan',9]}
df = pd.DataFrame(dictx).astype(float)

我正在尝试插入包含值nan"的各个段.
就上下文而言,我正在尝试使用城市(巴西圣保罗)提供的 GPS 数据来跟踪公交车速度,但数据很少,并且部分不提供信息,例如,但有部分我知道它们会停止,例如黎明,但信息也以nan"的形式出现.

I'm trying to interpolate various segments which contain the value 'nan'.
For context, I'm trying to track bus speeds using GPS data provided by the city (São Paulo, Brazil), but the data is scarce and with parts that do not provide the information, as the e.g., but there're segments which I know for a fact that they are stopped, such as dawn, but the information come as 'nan' as well.

我需要什么:
我一直在试验 dataframe.interpolate() 参数(limit 和 limit_diretcion),但结果很短.如果我设置 df.interpolate(limit=2) 我不仅会插入我需要的数据,还会插入不应该插入的数据.所以我需要在由限制定义的部分之间进行插值

What I need:
I've been experimenting with dataframe.interpolate() parameters (limit and limit_diretcion) but came up short. If I set df.interpolate(limit=2) I will not only interpolate the data that I need but the data where it shouldn't. So I need to interpolate between sections defined by a limit

期望的输出:

Out[7]:
    col1   col2   col3
0    1.0  20.00  15.00
1    nan  nan    nan
2    nan  nan    nan
3    nan  nan    nan
4    5.0  22.00  10.00
5    6.0  23.50  12.00
6    7.0  25.00  14.00
7    8.0  27.50  13.50
8    9.0  30.00  13.00
9    nan  nan    nan
10   nan  nan    nan
11   nan  nan    nan
12   13.0 25.00  9.00

我一直在尝试应用的逻辑基本上是试图找到 nan 并计算它们的索引之间的差异,因此创建一个新的 dataframe_temp 进行插值,然后将其添加到另一个创建新的 dataframe_final 中.但是由于 'nan'=='nan' 返回 False

The logic that I've been trying to apply is basically trying to find nan's and calculating the difference between their indexes and so createing a new dataframe_temp to interpolate and only than add it to another creating a new dataframe_final. But this has become hard to achieve due to the fact that 'nan'=='nan' return False

推荐答案

这是一个技巧,但可能仍然有用.可能 Pandas 0.23 会有更好的解决方案.

This is a hack but may still be useful. Likely Pandas 0.23 will have a better solution.

https://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#dataframe-interpolate-has-gained-the-limit-area-kwarg

df_fw = df.interpolate(limit=1)
df_bk = df.interpolate(limit=1, limit_direction='backward')

df_fw.where(df_bk.notna())

    col1  col2  col3
0    1.0  20.0  15.0
1    NaN   NaN   NaN
2    NaN   NaN   NaN
3    NaN   NaN   NaN
4    5.0  22.0  10.0
5    6.0  23.5  12.0
6    7.0  25.0  14.0
7    8.0  27.5  13.5
8    9.0  30.0  13.0
9    NaN   NaN   NaN
10   NaN   NaN   NaN
11   NaN   NaN   NaN
12  13.0  25.0   9.0

不是黑客
更合法的处理方式.
泛化处理任何限制.


Not a Hack
More legitimate way of handling it.
Generalized to handle any limit.

def interp(df, limit):
    d = df.notna().rolling(limit + 1).agg(any).fillna(1)
    d = pd.concat({
        i: d.shift(-i).fillna(1)
        for i in range(limit + 1)
    }).prod(level=1)

    return df.interpolate(limit=limit).where(d.astype(bool))

df.pipe(interp, 1)

    col1  col2  col3
0    1.0  20.0  15.0
1    NaN   NaN   NaN
2    NaN   NaN   NaN
3    NaN   NaN   NaN
4    5.0  22.0  10.0
5    6.0  23.5  12.0
6    7.0  25.0  14.0
7    8.0  27.5  13.5
8    9.0  30.0  13.0
9    NaN   NaN   NaN
10   NaN   NaN   NaN
11   NaN   NaN   NaN
12  13.0  25.0   9.0

还可以处理列与列之间 NaN 的变化.考虑一个不同的 df

Can also handle variation in NaN from column to column. Consider a different df

dictx = {'col1':[1,'nan','nan','nan',5,'nan','nan',7,'nan',9,'nan','nan','nan',13],\
    'col2':[20,'nan','nan','nan',22,'nan',25,'nan','nan',30,'nan','nan','nan',25],\
    'col3':[15,'nan','nan','nan',10,'nan',14,'nan',13,'nan','nan','nan',9,'nan']}
df = pd.DataFrame(dictx).astype(float)
df

    col1  col2  col3
0    1.0  20.0  15.0
1    NaN   NaN   NaN
2    NaN   NaN   NaN
3    NaN   NaN   NaN
4    5.0  22.0  10.0
5    NaN   NaN   NaN
6    NaN  25.0  14.0
7    7.0   NaN   NaN
8    NaN   NaN  13.0
9    9.0  30.0   NaN
10   NaN   NaN   NaN
11   NaN   NaN   NaN
12   NaN   NaN   9.0
13  13.0  25.0   NaN

然后用 limit=1

df.pipe(interp, 1)

    col1  col2  col3
0    1.0  20.0  15.0
1    NaN   NaN   NaN
2    NaN   NaN   NaN
3    NaN   NaN   NaN
4    5.0  22.0  10.0
5    NaN  23.5  12.0
6    NaN  25.0  14.0
7    7.0   NaN  13.5
8    8.0   NaN  13.0
9    9.0  30.0   NaN
10   NaN   NaN   NaN
11   NaN   NaN   NaN
12   NaN   NaN   9.0
13  13.0  25.0   9.0

并使用 limit=2

df.pipe(interp, 2).round(2)

     col1   col2  col3
0    1.00  20.00  15.0
1     NaN    NaN   NaN
2     NaN    NaN   NaN
3     NaN    NaN   NaN
4    5.00  22.00  10.0
5    5.67  23.50  12.0
6    6.33  25.00  14.0
7    7.00  26.67  13.5
8    8.00  28.33  13.0
9    9.00  30.00   NaN
10    NaN    NaN   NaN
11    NaN    NaN   NaN
12    NaN    NaN   9.0
13  13.00  25.00   9.0

这篇关于在由索引分隔的部分中插入 Pandas Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:01