繁体   English   中英

向后插入值到Pandas Dataframe(高索引到低索引)

[英]Insert Values into Pandas Dataframe backwards (High Index to low)

使用.fillna找到了解决方案

您可以猜到,我的头衔已经很混乱了,我也是! 我有一个这样的数据框

Index         Values
 0             NaN
 1             NaN
...................
230            350.21
231            350.71
...................
1605           922.24

在230和1605之间,我有值,但没有前229个条目的值。 因此,我计算了斜率以近似缺少的数据并将其存储在“斜率”中。

Y1 = df['Values'].min()
X1ID = df['Values'].idxmin()
Y2 = df['Values'].max()
X2ID = df['Values'].idxmax()
slope = (Y2 - Y1)/(X2ID - X1ID)

本质上,我想从“值”中获取.min,减去斜率并将新值插入到前一个.min之前的索引中。 但是,我完全迷路了,我尝试了如下操作:

 df['Values2'] = df['Values'].min().apply(lambda x: x.min() - slope) 

但这显然是垃圾。 我将不胜感激一些建议

编辑

因此,在尝试了多种方法之后,我发现了至少对我有用的粗略解决方案。

loopcounter = 0
missingValue = []
missingindex = []
missingindex.append(loopcounter)
missingValue.append(Y1)
for minValue in missingValue:
    minValue = minValue-slopeave
    missingValue.append(minwavelength)
    loopcounter +=1
    missingindex.append(loopcounter)
    if loopcounter == 230:
         break
del missingValue[0]
missingValue.reverse()
del missingindex[-1]

首先,我创建了两个列表,一个用于缺失值,另一个用于索引。 之后,我将最小值(Y1)添加到列表中并开始循环。 我希望循环在230次(丢失值的数量)后停止,每个循环将从列表中的项中减去斜率,从最小值开始,同时还将计数器添加到missingindex列表中。

删除第一个值并反转顺序将列表转换为正确的顺序。

missValue = dict(zip(missingindex,missingValue))

然后,我将两个列表合并为字典

df['Values'] = df['Values'].fillna(missValue)

之后,我使用.fillna函数用字典填充数据框。

这对我有用,我知道它可能不是最优雅的解决方案...

我要感谢所有花时间尝试帮助我的人,非常感谢。

检查一下。 但是,我认为您必须将其置于循环中,因为插入和最小值计算必须进行重新计算

import pandas as pd
import numpy as np

df = pd.DataFrame(columns=('Values',),data=
                    [
                        np.nan,
                        np.nan,
                        350.21,
                        350.71,
                        922.24
                    ])

Y1 = df['Values'].min()
X1ID = df['Values'].idxmin()
Y2 = df['Values'].max()
X2ID = df['Values'].idxmax()
slope = (Y2 - Y1)/(X2ID - X1ID)

line = pd.DataFrame(data=[Y1-slope], columns=('Values',), index=[X1ID])
df2 = pd.concat([df.ix[:X1ID-1], line, df.ix[X1ID:]]).reset_index(drop=True)
print df2

此处提供了插入逻辑。 是否可以使用熊猫在数据框中的任意位置插入一行?

我认为您可以将locinterpolate使用:

print df
       Values
Index        
0         NaN
1         NaN
2         NaN
3         NaN
4         NaN
5         NaN
6         NaN
229       NaN
230    350.21
231    350.71
1605   922.24

#add value 0 to index = 0
df.at[0, 'Values'] = 0
#add value Y1 - slope (349.793978) to max NaN value 
df.at[X1ID-1, 'Values'] = Y1 - slope
print df
           Values
Index            
0        0.000000
1             NaN
2             NaN
3             NaN
4             NaN
5             NaN
6             NaN
229    349.793978
230    350.210000
231    350.710000
1605   922.240000
print df.loc[0:X1ID-1, 'Values']
Index
0        0.000000
1             NaN
2             NaN
3             NaN
4             NaN
5             NaN
6             NaN
229    349.793978
Name: Values, dtype: float64

#filter values by indexes and interpolate
df.loc[0:X1ID-1, 'Values'] = df.loc[0:X1ID-1, 'Values'].interpolate(method='linear')
print df
           Values
Index            
0        0.000000
1       49.970568
2       99.941137
3      149.911705
4      199.882273
5      249.852842
6      299.823410
229    349.793978
230    350.210000
231    350.710000
1605   922.240000

我将对此进行一点修改:

df['Values2'] = df['Values']
df.ix[df.Values2.isnull(), 'Values2'] = (Y1 - slope)

编辑

或者尝试将其放入如下所示的循环中。 这将递归地填充所有值,直到达到序列的结尾:

def fix_rec(series):
    Y1 = series.min()
    X1ID = series.idxmin() ##; print(X1ID)
    Y2 = series.max()
    X2ID = series.idxmax()
    slope = (Y2 - Y1) / (X2ID - X1ID);

    if X1ID == 0: ## termination condition
        return series

    series.loc[X1ID-1] = Y1 - slope

    return fix_rec(series)

这样称呼它:

df['values2'] = df['values']
fix_rec(df.values2)

希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM