Python ValueError:invalid literal for int(),数据类型转换问题求助
Hey there! Let's work through that data type conversion error you're facing. The root issue is simple: your value column contains decimal strings like '86.244', and you can't convert these directly to integers—Python doesn't know how to parse a string with a decimal point into an int without first handling the decimal part.
Solution 1: Convert Columns to Their Correct Target Types
Instead of trying to convert both columns to integers in one step (or chaining float → int for all columns), specify the exact type each column needs using a dictionary with astype():
# Select the relevant columns and convert each to the appropriate type df_sub = df_data[['value', 'year']].astype({ 'value': float, # Keep decimal precision (critical for productivity metrics) 'year': int # Year should be an integer for indexing })
This preserves the meaningful decimal values in value while converting year to a proper integer for sorting and indexing.
Solution 2: If You Need Integer Values for value (Truncate Decimals)
If you specifically need whole numbers for value (e.g., rounding or truncating decimals), handle the column conversion separately to avoid confusion:
df_sub = df_data[['value', 'year']].copy() # First convert value to float, then to int (truncates the decimal portion) df_sub['value'] = df_sub['value'].astype(float).astype(int) # Convert year directly to integer df_sub['year'] = df_sub['year'].astype(int)
Note: This will lose the decimal precision from your original data—only use this if you're certain you don't need those decimal values for your analysis.
One Quick Fix for Your Plot
I noticed your code ends with mp.show instead of mp.show()—you need the parentheses to actually render the plot in Jupyter. Update that line to:
mp.show()
Why Your Original Code Failed
The error ValueError: invalid literal for int() with base 10: '86.244' occurs because df_data[['value', 'year']].astype(int) tries to convert the decimal string '86.244' directly to an integer. Python can't parse a string with a decimal point as a base-10 integer, hence the failure.
内容的提问来源于stack exchange,提问作者FrenchConnections

