属性错误:';方法描述符';对象没有属性';df#U新';在python中替换字符串时

属性错误:';方法描述符';对象没有属性';df#U新';在python中替换字符串时,python,pandas,str-replace,Python,Pandas,Str Replace,我正在编写一段简短的代码,以删除dataframe中一列数据中名称的web浏览器版本号。i、 e.将包含字母和数字字符的字符串仅替换为字母字符 我写过: df_new=(rename()) str.replace.df_new[new_browser]('[.*0-9]',"",regex=True) 我收到这个错误消息,我不明白它告诉我什么 AttributeError Traceback (most recent

我正在编写一段简短的代码,以删除dataframe中一列数据中名称的web浏览器版本号。i、 e.将包含字母和数字字符的字符串仅替换为字母字符

我写过:

df_new=(rename())

str.replace.df_new[new_browser]('[.*0-9]',"",regex=True)
我收到这个错误消息,我不明白它告诉我什么

AttributeError                            Traceback (most recent call last)
<ipython-input-4-d8c6f9119b9f> in <module>
      3 df_new=(rename())
      4 
----> 5 str.replace.df_new[new_browser]('[.*0-9]',"",regex=True)

AttributeError: 'method_descriptor' object has no attribute 'df_new'
当我下次调用数据帧更新时,数据帧更新无法识别,这使我遇到了麻烦。通常在JN中,我只是继续向df写入修改,它保留更新。但是,即使是代码
df_new.head(1)
也需要这样编写,以便在运行第一个函数后,出于某种原因(尽管错误消息不同,但这感觉像是一个类似的问题):

有人能帮我吗

最好的


Miriam

错误告诉您没有正确使用
Series.str.replace()
方法

你有:

str.replace.df_new[new_browser]('[.*0-9]','',regex=True)

当您想要的是:

df_new[new_browser].str.replace('[.*0-9]','',regex=True)

见此:

>>将熊猫作为pd导入
>>>
>>>s=pd.系列(['a1',b2',c3',d4'])
>>>s.str.replace('[.*0-9]','',regex=True)
0 a
1b
2 c
三维
数据类型:对象
并将其与此进行比较(这是您得到的,
s
这是您的
df_new[new_browser]
):

>>将熊猫作为pd导入
>>>
>>>s=pd.系列(['a1',b2',c3',d4'])
>>>str.replace.s('[.*0-9]','',regex=True)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:'method\u descriptor'对象没有属性's'

谢谢,这真的很有帮助。我在这里读了一些其他的问答,但我不得不承认这是全部替换,而不是str.replace
import pandas as pd
import numpy as np
import re

    #write a function to change column names using a dictionary
    
    def rename():
        dict_col = {'Browser':'new_browser', 'Page':'web_page', 'URL':'web_address', 'Visitor ID':'visitor_ID'}
        df = pd.read_csv ('dummy_webchat_data.csv')
        for y in df.columns:
            if y in dict_col:
                df_new=df.rename(columns={y:dict_col}[y])
            
        return df_new

rename()
df_new=(rename())
df_new.head(1)