Filling Missing values Pandas Dataframe by specifi

2019-08-02 21:33发布

I have a dataset and I want to fill the missing data in the column 'value' with bfill with adding a string to it. Here is to code that I have:

import pandas as pd
import numpy as np 
df = pd.DataFrame(
    {
        'category': ['X', 'X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y'],
        'name': ['A','A', 'B','B','B','B', 'C','C','C'],
        'other_value': [10, np.nan, np.nan, 20, 30, 10, 30, np.nan, 30],
        'value': [1, np.nan, np.nan, 2, 3, 1, 3, np.nan, 3],
    }
)
print(df)

def fillValue(g):

    gNotNull = g.dropna()
    wtAvg = str(gNotNull[0])+'5D'
    return g.fillna(wtAvg)



ff=pd.DataFrame()
ff["value"] = df['value'].transform(fillValue)
ff

The output that I am getting from this code is:

value
0
1 
1
1.05D 
2
1.05D 
3
2 
4
3 
5
1 
6
3 
7
1.05D 
8
3 

the out put that I want is to get back filled and look something like this:

value
0
1 
1
25D 
2
35D 
3
2 
4
3 
5
1 
6
3 
7
85D 
8
3 

I appreciate if anyone can help. Thanks

1条回答
贪生不怕死
2楼-- · 2019-08-02 21:50

IIUC

s=df.value.bfill()
s.loc[df.value.isnull()]=s.astype(int).astype(str)+'5D'
s
Out[771]: 
0      1
1    25D
2    25D
3      2
4      3
5      1
6      3
7    35D
8      3
Name: value, dtype: object
查看更多
登录 后发表回答