Trying to expand row data and convert to DataFrame, getting this error: AttributeError: 'float' object has no attribute 'keys'

I'm facing the following error when running my function below: AttributeError: 'float' object has no attribute 'keys'

The following data is contained in each row:

{'volume': '8579152.41', 'price_change': '-0.00211948', 'price_change_pct': '-0.0016', 'volume_change': '445336.65', 'volume_change_pct': '0.0548', 'market_cap_change': '-2813342.71', 'market_cap_change_pct': '-0.0016'}

def adj_col(data,column_name): # Function to expand columns open up brace brackets and then converts it to a DataFrame
    convert = data.pop(column_name).values.tolist()
    converted = pd.DataFrame(convert)
    return converted

My goal is to expand the data into a column and once this is completed to convert it into a DataFrame.

What am I doing wrong?

1 answer

  • answered 2021-05-15 17:25 Andrej Kesely

    You have probably NaN values somewhere in the column. This example will filter out the NaN values:

    convert = df.pop("Values").values.tolist()
    converted = pd.DataFrame([v for v in x if pd.notna(v)])  # <-- filter out the NaN values
    print(converted)