简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM