首页 > 解决方案 > 读取数据框的困难

问题描述

我有一个csv文件如下:

[8:3:1978] LOG [Sale:internals.py:makeSaleEntry:0] Entered with productid= 2327, storeid= 146, No.OfUnits= 1
[19:1:2007] LOG [Sale:internals.py:makeSaleEntry:1] Entered with productid= 1908, storeid= 202, No.OfUnits= 11
[22:4:2001] LOG [Sale:internals.py:makeSaleEntry:2] Entered with productid= 3072, storeid= 185, No.OfUnits= 16
[22:12:1915] LOG [Sale:internals.py:makeSaleEntry:3] Entered with productid= 1355, storeid= 177, No.OfUnits= 1
[19:8:1963] LOG [Sale:internals.py:makeSaleEntry:4] Entered with productid= 2235, storeid= 35, No.OfUnits= 16
[16:11:1997] LOG [Sale:internals.py:makeSaleEntry:5] Entered with productid= 1439, storeid= 141, No.OfUnits= 26

我已经使用数据框 df 读取了文件。

df = pd.read_csv('a.txt')

我想逐行访问数据框并在另一个 csv 文件中打印 productid、storeid 和 No.OfUnits,如下所示:

2327,146,1
1908,202,11
3072,185,16
1355,177,1
2235,35,16
1439,141,26

我的一段代码是:

for i, row in df.iterrows():
    strr = ""
    for j, column in row.iteritems():
        seq = column.split('= ')
        strr = strr + seq[1] + ","
    file = open("a.csv", "a")
    file.write(strr[:-1]+"\n")
    file.close()

这段代码工作正常,只是它忽略了写第一行 (2327,146,1)。请提出更改建议。

标签: pythonpandas

解决方案


您的代码忽略了第一行,因为默认情况下read_csv假定它是标题。您可以通过按照上面的建议添加来使您的原始代码工作header=None。您可能还想考虑使用正则表达式来提取值的更具可读性的版本。

df = pd.read_csv('a.txt', header=None)
df['productid'] = df[0].str.findall('productid= ([0-9]+)').apply(lambda l: l[0])
df['storeid'] = df[1].str.findall('storeid= ([0-9]+)').apply(lambda l: l[0])
df['No.OfUnits'] = df[2].str.findall('No.OfUnits= ([0-9]+)').apply(lambda l: l[0])
df1 = df.loc[:, ['productid', 'storeid', 'No.OfUnits']]
df1.to_csv('a.csv', header=False, index=False, mode='a')

顺便说一句,熊猫对此并不真正需要。这也将起作用:

import re
with open('a.txt') as f:
    values = [re.findall('productid= ([0-9]+), storeid= ([0-9]+), No.OfUnits= ([0-9]+)', 
                        line)[0] for line in f]
with open('a.csv', 'a') as f:
    for v in values:
        f.write(','.join(v) + '\n')

推荐阅读