我有一個數據框,當我嘗試計算 pct_change() 時,它顯示了一個錯誤 TypeError: unsupported operand type(s) for /: 'str' and 'float'。然后我嘗試將類型轉換為 float,它向我顯示ValueError: could not convert string to float:unemployment_df['Unemployment_pct_change'] = unemployment_df['Unemployment_Value'].pct_change()
unemployment_df['Unemployment_Value']=unemployment_df['Unemployment_Value'].astype(float)但不知道弦在哪里,是什么弦?所以我試圖找到單元格索引,其中單元格值是字符串而不是數字。怎么做?謝謝
2 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
astype(float)
可能有效,否則還有一個選擇
unemployment_df['Unemployment_Value']=pd.to_numeric(unemployment_df['Unemployment_Value'])
然后你可以計算 pct_change
unemployment_df['Unemployment_pct_change'] = unemployment_df['Unemployment_Value'].pct_change()

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
Pandas 并沒有真正區分比object
.
df[df['col']].apply(lambda x: isinstance(x, str))
將為您提供列中包含字符串的行'col'
然后您可以按照自己的意愿清理它們。
添加回答
舉報
0/150
提交
取消