我有一个来自csv文件的标题列表,但其中一些有(,a),我想将其删除为becaome(a),然后将新标题添加到新列表中,但出现错误
代码:
import pandas as pd
import string
new_title =[]
alphabests = list(string.ascii_letters) # this list invlude all alphabets
title_file = pd.read_csv('D:\\titles.csv',sep=';')
title = title_file['title']
x=0 #move in titles
while x < len(title):
y=0 #move in alphabets
while y < len(alphabests):
check_about_alpabets = [w.replace(',{}'.format(alphabests[y]), '{}'.format(alphabests[y])) for w in title[x]]
y +=1
new_title.append(title[x])
x +=1
错误
TypeError: 'float' object is not iterable
我将建议进行两次编辑,这可能有助于您找出错误: 首先,不要使用while循环,而是使用for循环,并迭代标题列表中的项目。 在尝试处理错误之前,请尝试打印该列表中的变量, 可能有一个浮动类型的标题-表示一个数字,您无法对其进行迭代(或尝试删除特殊字符)。 如果有这样一个标题,请尝试使用If语句创建两个条件,以不同方式处理每种类型,我的意思如下
可以对发布的代码进行两项改进
代码
测试
不需要的逗号示例:
所需逗号的示例:
创建数据集
清理前的数据集
清理后的数据集
出现此错误是因为变量“title”是Pandas中的系列对象,而不是列表。 如果要更改数据帧中的列名,可以执行以下操作:
但在将inplace参数设置为True之前,只需检查输出
相关问题 更多 >
编程相关推荐