向后将值插入数据帧(从高索引到低索引)

2024-09-27 07:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

找到了一个使用.fillna的解决方案

你可以猜到,我的头衔已经让人困惑了,我也是! 我有一个这样的数据帧

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

在230和1605之间,我有值,但不适用于前229个条目。所以我计算了斜率来近似丢失的数据,并将其存储在“slope”中。你知道吗

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函数用字典填充我的数据帧。你知道吗

这对我很有效,我知道这可能不是最优雅的解决方案。。。你知道吗

我要感谢所有花时间帮助我的人,非常感谢。你知道吗


Tags: 数据df列表解决方案minslopevaluesappend
3条回答

看看这个。不过,我觉得你要把这是一个循环,因为插入和最小值的计算必须做重新计算

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

这里提供了插入逻辑Is it possible to insert a row at an arbitrary position in a dataframe using pandas?

我想你可以用^{}^{}

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)

我希望这有帮助!你知道吗

相关问题 更多 >

    热门问题