df转np,一直提示类型错误:invalid literal for int() with base 10: 'abc'
import pandas as pd
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
df1=pd.DataFrame({'name':["abc","xyz"],'age':[21,18],'marks':[50,75]})
df1["name"] = df1["name"].astype("string")
print(df1)
print(df1.dtypes)
arr01 = df1.to_numpy() # ok
print(arr01)
arr01 = arr01.astype(student) # nok
print(arr01.dtype)
运行结果如下:#####
name age marks
0 abc 21 50
1 xyz 18 75
##############
name string
age int64
marks int64
dtype: object
##############
[[‘abc’ 21 50]
[‘xyz’ 18 75]]
object
##############
报错信息:arr01 = arr01.astype(student) # nok
ValueError: invalid literal for int() with base 10: ‘abc’