Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Must specify a fill 'value' or 'method'

For a current project, I am planning to clean a Pandas DataFrame off its Null values. For this purpose, I want to use pd.DataFrame().fillna(), which is apparently a solid soluton for data cleanups.

When running the below code, I am however receiving the following error ValueError: Must specify a fill 'value' or 'method'. I tried several options to rewrite the line df = pd.DataFrame().fillna(), none of which led to the desired outcome.

Is there any smart tweak to get this running?

import string
import json
import pandas as pd

# Loading and normalising the input file
file = open("sp500.json", "r")
data = json.load(file)
df = pd.json_normalize(data)
df = pd.DataFrame().fillna()
like image 919
Malte Susen Avatar asked Sep 17 '25 05:09

Malte Susen


1 Answers

fillna() takes parameters to determine how to replace the values, as stated in the documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.fillna.html#pandas.DataFrame.fillna

DataFrame.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None) → Union[ForwardRef(‘DataFrame’), NoneType]

The error you get ValueError: Must specify a fill 'value' or 'method'. tells that you must at least give one of the value or method parameter. The former being a constant value to set to all null entries, the latter being a method to fill the entries.

For your second remark, your dataframe object will be modified if you set the parameter inplace=True, otherwise the method will return a new dataframe object.

like image 176
aveuiller Avatar answered Sep 19 '25 19:09

aveuiller