我有以下表测试
Work_Experience
1
2
3
4
3 Month
4 months
5 Month
现在我想创建一个新的列IntOnly其中前四个值(数据类型Int64)应该在那里,剩下的最后3行应该显示'NaN'
我已经尝试了两个主要代码
Test['IntOnly'] = Test['Work_Experience'].select_dtypes(include='number')
它抛出'Series'对象没有属性'select_dtypes'
IntOnly=[]
for i in Test.Work_Experience:
if Test[i].dtype == 'int64':
IntOnly.append(i)
它抛出键错误:1
下面应该是输出
Work_Experience IntOnly
1 1
2 2
3 3
4 4
3 Month NaN
4 months NaN
5 Month NaN
试试这个:
df = pd.DataFrame({
"Work_Experience": [1,2,3,4,'3 Month','4 months','5 Month']
})
df["IntOnly"] = pd.to_numeric(df["Work_Experience"], errors='coerce')
df