【问题标题】:How to group subsequent rows with identical keys in a CSV file如何在 CSV 文件中对具有相同键的后续行进行分组
【发布时间】:2016-03-23 20:22:13
【问题描述】:

如果 col1 等于之前行中的相同值,我正在尝试连接 col3,然后将输出写入新文件。我有一个如下所示的 CSV 文件:

col1,col2,col3
a,12,"hello "
a,13,"good day"
a,14,"nice weather"
b,1,"cat"
b,2,"dog and cat"
c,2,"animals are cute"

我想要的输出:

col1,col3
a,"hello good day nice weather"
b,"cat dog and cat"
c,"animals are cute"

这是我尝试过的:

import csv

with open('myfile.csv', 'rb') as inputfile, open('outputfile.csv','wb') as outputfile:
    reader=csv.reader(inputfile)
    writer=csv.writer(outputfile)
    next(reader)
    for row in reader:
        while row[0]==row[0]:
            concat_text=" ".join(row[2])
        print concat_text
        writer.writerow((row[0],concat_text))

它运行但我没有输出。帮助表示赞赏。

【问题讨论】:

  • while row[0]==row[0]: ... 永远不会前进,这是一个无限循环。

标签: python string csv


【解决方案1】:

如果您对使用 pandas 感兴趣,可以将您的 DataFrame 分组,然后输出唯一值:

import pandas as pd

df = pd.read_csv('test.txt')
print(df)

您的原始数据帧

  col1  col2              col3
0    a    12            hello 
1    a    13          good day
2    a    14      nice weather
3    b     1               cat
4    b     2       dog and cat
5    c     2  animals are cute

第二个DataFrame

df2 = df.groupby(df['col1'])
df2 = df2['col3'].unique()
df2 = df2.reset_index()

print(df2)

将导致:

  col1                              col3
0    a  [hello , good day, nice weather]
1    b                [cat, dog and cat]
2    c                [animals are cute]

要连接第三列,您需要像这样使用apply

df2['col3'] = df2['col3'].apply(lambda x: ' '.join(s.strip() for s in x))

  col1                          col3
0    a   hello good day nice weather
1    b               cat dog and cat
2    c              animals are cute

完整代码:

import pandas as pd

df = pd.read_csv('test.txt')
df2 = df.groupby(df['col1'])

df2 = df2['col3'].unique()
df2 = df2.reset_index()

df2['col3'] = df2['col3'].apply(lambda x: ' '.join(s.strip() for s in x))

df2.to_csv('output.csv')

【讨论】:

  • 那是因为hello在原始数据后面有一个空格。
  • @Leb 记得加df2.to_csv('somefile.csv')
  • @Ilja,确实如此。谢谢。
  • 谢谢,我认为 pandas 也是这样做的另一种好方法
  • 不客气。这个答案只是作为您和任何可能的未来观众的替代方案。如果您无法使用pandas,则此处的其他答案是正确的。
【解决方案2】:
import csv

with open('myfile.csv', 'rb') as inputfile, open('outputfile.csv', 'wb') as outputfile:
    reader=csv.reader(inputfile)
    writer=csv.writer(outputfile)
    prior_val = None
    text = []
    for line in reader:
        if line[0] == prior_val:
            text.append(line[2])
        else:
            if text:
                writer.writerow([prior_val, " ".join(text)])
            prior_val = line[0]
            text = [line[2]]
    if text:
        writer.writerow([prior_val, " ".join(text)])

>>> !cat outputfile.csv
col1,col3
a,hello  good day nice weather
b,cat dog and cat
c,animals are cute

>>> pd.read_csv('outputfile.csv', index_col=0)
                          col3
col1                              
a     hello  good day nice weather
b                  cat dog and cat
c                 animals are cute

【讨论】:

    【解决方案3】:

    问题是您将同一行与自身进行比较。此版本将最后一行与当前行进行比较。输出没有引号分隔,但它是正确的。 script.py的内容

    #!/usr/bin/env python
    
    import csv
    
    with open('myfile.csv', 'rb') as inputfile, open('outputfile.csv','wb') as outputfile:
        reader=csv.reader(inputfile)
        writer=csv.writer(outputfile)
        next(reader)
        lastRow = None
        # assumes data is in order on first column
        for row in reader:
            if not lastRow:
                # start processing line with the first column and third column
                concat_text = row[2].strip()
                lastRow = row
                print concat_text
            else:
                if lastRow[0]==row[0]:
                    # add to line
                    concat_text = concat_text + ' ' + row[2].strip()
                    print concat_text
                else:
                    # end processing
                    print concat_text
                    writer.writerow((lastRow[0],concat_text))
                    # start processing
                    concat_text = row[2]
                    print concat_text
                lastRow = row
        # write out last element
        print concat_text
        writer.writerow((lastRow[0],concat_text))
    

    运行 ./script.py 后输出文件.csv 的内容

    a,hello good day nice weather
    b,cat dog and cat
    c,animals are cute
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2013-02-05
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多