AttributeError:'str'对象没有属性'astype'

2024-09-30 01:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

对python和pandas来说是新的。我确信我犯了很多python和编程罪行。此脚本在我的个人电脑上运行,但我的工作电脑运行它。但我的google fu很弱,无法找到解决方案让它工作,也无法找出iv以前工作时做错了什么。 我得到这个错误

Traceback (most recent call last):
  File "A:\Python\doforms_data\Concept_script_working.pyw", line 1542, in <module>
    main()
  File "A:\Python\doforms_data\Concept_script_working.pyw", line 133, in main
    machine_refactory.apply(lambda x : x.astype(str)+' '+x.name) #
  File "C:\Users\agarth\AppData\Roaming\Python\Python38\site-packages\pandas\core\series.py", line 4045, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/lib.pyx", line 2228, in pandas._libs.lib.map_infer
  File "A:\Python\doforms_data\Concept_script_working.pyw", line 133, in <lambda>
    machine_refactory.apply(lambda x : x.astype(str)+' '+x.name) #
    AttributeError: 'str' object has no attribute 'astype'

这里是代码部分

   machine_refactory_loc=df.columns.get_loc(1629)
   machine_refactory=df.iloc[0:,(machine_refactory_loc):(machine_refactory_loc+2)]
   machine_refactory=machine_refactory.astype(str)
   machine_refactory.rename(columns = {1629:'Material'}, inplace = True) ## or Brick(1),Block(0)
   machine_refactory.rename(columns = {1630:'Roof'}, inplace = True) ## Flat(1),Arched(0)
   machine_refactory['Material'].replace('0','Block', inplace=True)
   machine_refactory['Material'].replace('1','Brick', inplace=True)
   machine_refactory['Roof'].replace('0',' - Arched Roof', inplace=True)
   machine_refactory['Roof'].replace('1',' - Flat Roof', inplace=True)
   machine_refactory=machine_refactory.astype(str)
   machine_refactory['Refractory (Blocked or Bricked) Cremator']=machine_refactory['Material']+ machine_refactory['Roof']
   machine_refactory=machine_refactory['Refractory (Blocked or Bricked) Cremator']
   machine_refactory.apply(lambda x : x.astype(str)+' '+x.name) #
   print(machine_refactory)

Tags: lambdaintruepandaslinemachinelocfile
1条回答
网友
1楼 · 发布于 2024-09-30 01:33:46

之后

machine_refactory=machine_refactory['Refractory (Blocked or Bricked) Cremator']

machine_refactory是一个系列,因此

machine_refactory.apply(lambda x : x.astype(str)+' '+x.name)

x内的lambda接受单元格值,这些值是字符串,因此x没有astypename属性。你想要:

machine_refactory.apply(lambda x: str(x) + machine_refactory.name]

相关问题 更多 >

    热门问题