pandas将细胞值增加0.1

时间:2018-04-09 14:59:10

标签: pandas

我有一个类似于下面的pandas数据框:

    A
0   1
1   NaN
2   2
3   NaN
4   NaN
5   3
6   4
8   NaN
9   5
10  NaN

我想要的是:

    A
0   1
1   1.1
2   2
3   2.1
4   2.2
5   3
6   4
8   4.1
9   5
10  5.1

我希望逐渐填充缺失值0.1。我一直在玩np.arrange,但我无法弄清楚如何将所有东西拼凑在一起。我觉得我走的是正确的道路,但我会感激一些帮助。谢谢

In []: import pandas as pd
In []: import numpy as np

In []: np.arange(1, 2, 0.1)
Out[]: array([1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])

In []: def up(x):                                                                     
           return x.astype(str) + '.' + np.arange(len(x)).astype(str)

In []: data = pd.DataFrame([[1,0],[0,1],[1,0],[0,1]], columns=["A", "B"])

In []: out = data.apply(up).values
array([['1.0', '0.0'],
       ['0.1', '1.1'],
       ['1.2', '0.2'],
       ['0.3', '1.3']], dtype=object)

In []: df = pd.DataFrame(out)
     A    B
  0  1.0  0.0
  1  0.1  1.1
  2  1.2  0.2
  3  0.3  1.3

1 个答案:

答案 0 :(得分:0)

有点难以得到那一点

s=df.A.isnull().astype(int).diff().ne(0).cumsum()[df.A.isnull()]# creat the group Id for those NaN value , if they are NaN they belong to same Id

df.A.fillna(df.A.ffill()+s.groupby(s).cumcount().add(1).mul(0.1))# then we using fillna , and creat the position inorder to adding the .01 for each

Out[1764]: 
0     1.0
1     1.1
2     2.0
3     2.1
4     2.2
5     3.0
6     4.0
8     4.1
9     5.0
10    5.1
Name: A, dtype: float64