-1

I have a data sets which has column with missing value 2439. But the missing value is such that for specific index has some missing value and some fill value as shown below (Compare column 'Item_Identifier' and 'Item_Weight')

enter image description here

If carefully seen for specific item_identifier, there missing value in item_weight. Like this there many more Item_Identifier which missing value. Is there any way using python we fill missing value for only item_weight as same.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Kedar17
  • 178
  • 2
  • 14

2 Answers2

0

You can make the table into a pandas DataFrame then df['item_weight'].fillna(15.5, inplace=True)

lintang
  • 100
  • 5
0

Reproducible example:

df = pd.DataFrame({'col1': ['a', 'a', 'b','b', 'b', 'c'], 
                   'col2': [10, np.nan, np.nan, np.nan, 20, 30]})

    col1    col2
0   a       10.0
1   a       NaN
2   b       NaN
3   b       NaN
4   b       20.0
5   c       30.0

You can groupby your col1 and agg using first

vals = df.groupby('col1').agg('first')

    col2
col1    
a   10.0
b   20.0
c   30.0

Then just use same indexing and fillna() to match and fill the values

df = df.set_index('col1').fillna(vals).reset_index()

    col1    col2
0   a       10.0
1   a       10.0
2   b       20.0
3   b       20.0
4   b       20.0
5   c       30.0
rafaelc
  • 57,686
  • 15
  • 58
  • 82