【问题标题】:Apply dictionary to dataframe column containing strings将字典应用于包含字符串的数据框列
【发布时间】:2021-05-14 00:53:01
【问题描述】:

我正在尝试将字典应用于包含字符串行的数据框列,例如:

0 [一些、文本等...

1 [其他,文字...

并使用了以下代码,该代码适用于单个字符串,但不适用于数据框:

dict = pd.read_csv(r'C:\Users\dictionary.csv', header=None, dtype={0: str}).set_index(0).squeeze().to_dict()

def dictionary(text):
    for x in dict:
        for y in text:
            if dict[x][0] == y:
                        y == x[1]
            
                        text = ' '.join(dict.get(y, y) for y in text)
                        return
                
df['Processed'] = df['Processed'].apply(dictionary)
df['Processed']

产生以下错误

<ipython-input-75-decedde15a18> in dictionary(text)
      5 def dictionary(text):
      6     for x in dict:
----> 7         for y in text:
      8             if dict[x][0] == y:
      9                         y == x[1]

TypeError: 'NoneType' object is not iterable

感谢任何关于我做错了什么的帮助/指导!谢谢

【问题讨论】:

    标签: pandas dictionary normalization


    【解决方案1】:

    如果您的数据系列中有 NaN 值,则会出现此错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2021-09-17
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多