2016-03-23 16 views
1

Eğer col1, satırdaki aynı değere eşitse ve sonra çıkışı yeni bir dosyaya yazıyorsa col3'ü yapmaya çalışıyorum.Bir CSV dosyasında aynı anahtarlarla sonraki satırların nasıl gruplandırılacağı

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" 

çıkış istediğim:

Bu şimdiye denedim budur: Şöyle bir CSV dosyası var

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)) 

O çalıştırır ama hiçbir çıkış var. Yardım aldım. Eğer grup sizin DataFrame sonra çıkış benzersiz değerler can pandas kullanarak ilgileniyorsanız

+3

içeriğine ./script.py çalıştırdıktan sonra 'ederken satır [0] == satır [0]: ...' asla ilerlemek, sonsuz bir döngüdür. –

cevap

3

:

import pandas as pd 

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

Orijinal DataFrame ikinci DataFrame

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 

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

print(df2) 

Çıkacaksınız:

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

üçüncü sütun birleştirmek için, aşağıdakiler gibi apply kullanmanız gerekir:

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 

Komple kodu:

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') 
+0

"merhaba" dan sonra iki boşluk var, ama yeterince iyi, –

+0

derdim. Çünkü "merhaba" orijinal verinin ardından bir boşluk buldu. – Leb

+0

@Leb, df2.to_csv ('somefile.csv') ' –

1
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 
0

Sorun, aynı satırı kendi ile karşılaştırıyordunuz. Bu sürüm, son satırı geçerli satırla karşılaştırır. Çıktı, sınırlandırılmış bir alıntı değildir, ancak doğrudur. script.py İçeriği

#!/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)) 

outputfile.csv

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