Filling Missing values Pandas Dataframe by specific value

假如想象 提交于 2019-12-11 08:45:54

问题


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:


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


来源:https://stackoverflow.com/questions/49052270/filling-missing-values-pandas-dataframe-by-specific-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!