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
IIUC