我是Python新手。我有一个CSV文件,我正在解析,并希望返回符合两个条件的信息。
这些数据包含有关消费金融产品和服务的投诉。16列在文件中,我希望满足其中的两个条件,“产品”和“及时响应”。我希望得到对最常见的“产品”的及时响应的百分比。“产品”包含9个产品,“及时响应”是“是”/“否”字段。
我使用itemgetter()返回对产品抱怨最多的产品:
for row in reader:
id_counts = Counter(map(itemgetter(1), reader))
pprint (id_counts)返回:
Counter({'Credit reporting, credit repair services, or other personal consumer reports': 112,
'Debt collection': 32,
'Mortgage': 12,
'Credit card or prepaid card': 11,
'Checking or savings account': 11,
'Student loan': 5,
'Money transfer, virtual currency, or money service': 4,
'Vehicle loan or lease': 4,
'Payday loan, title loan, or personal loan': 1})我现在期待着对最常见的投诉作出及时的反应。
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c = Counter(map(itemgetter(15), reader))
print (c)Counter({'Yes': 186, 'No': 4})这是不正确的,并且正在从所有字段中计算是/否。
我也试过:
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c = Counter(row[15].split())
print (sum(c))返回不支持的操作数错误。
我想使用getitem或抗衡来解决这个解决方案,因为这就是我开始的地方,但是任何帮助/建议都是非常感谢的。
发布于 2022-11-10 18:54:45
c = Counter(map(itemgetter(15), reader))您正在从reader读取所有(剩余的)元素,但希望只从当前的row读取。
你可以用itemgetter解决这个问题;
c = Counter()
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c.update([itemgetter(15)(row)])
print (c)..。但这比显而易见的要复杂得多。
c = Counter({"Yes": 0, "No": 0})
for row in reader:
if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
c[row[15]] += 1
print (c)也许这里最重要的观察是,你一次只看一行。您的逻辑似乎假设您正在检查所有匹配的行,但实际上您只是一个一个地遍历它们,然后依次检查每个行。
..。如果你想要花哨,你可以说
c = Counter(
map(itemgetter(15),
filter(lambda row: row[1] == 'Credit reporting, credit repair services, or other personal consumer reports',
reader)))https://stackoverflow.com/questions/74393910
复制相似问题