从数字列Pandas中删除字符串值?

时间:2019-12-07 23:48:00

标签: python database pandas dataframe

我希望从“平均评分”列中删除“字符串”值,能帮上什么忙吗? enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

假设您的表格是以下简化版本,

from io import StringIO
import pandas as pd, numpy as np

input = """ 
 bookID,title,average_rating
 1,Foo,1.5 
 2,Bar,2.5 
 3,Baz,"Unwanted string" 
 4,Boo,3.5 
 5,Goo,"Another string" 
 """
df = pd.read_csv(StringIO(input))

您可以使用以下命令删除average_rating列中所有非数字字符串值的行。

df[np.isfinite(pd.to_numeric(df.average_rating, errors="coerce"))]

答案 1 :(得分:1)

这是另一个例子

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

df = pd.read_excel('goodreadsdata.xlsx')

df[['average_rating']] = df[['average_rating']].apply(pd.to_numeric, errors='coerce')
# ALl Df
print(df)
# Average column average_rating without Nan Value
print (df['average_rating'].mean())