AttributeError: 'str' object has no attribute 'astype'

AttributeError: 'str' object has no attribute 'astype'

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)

之后

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

machine_refactory是一个系列,所以当你做

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

xlambda 中采用单元格值,它们是字符串,因此 x 没有 astype 也没有 name 属性.你想要:

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