简体   繁体   中英

Python Array To Csv, Group data from a CSV file by field value and save csv

i have a csv file

col1      col2
a          1     
a          2        
a          3    
b          3      
b          6    
b          1  

i use this code

import csv

result = {}

with open('data.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in csvreader:
        if row[0] in result:
            result[row[0]].append(row[1])
        else:
            result[row[0]] = [row[1]]

print result

output 
result:{
    'a': ['1', '2', '3'], 
    'b': ['3', '6', '1'] 
}

Now i want save this result to csv file(i want use this method) result.csv

ab
1 3
2 6
3 1

SOLVED

df_zor = pd.read_csv('5_gecici_.csv', encoding = "windows-1250", sep=';')
df = df_zor.groupby("gorevli").agg(lambda x: x.unique().tolist()).T
df.apply(lambda x: np.pad(x.iloc[0], (0, df.apply(lambda x: len(x.iloc[0]), axis=0).max() - len(x.iloc[0])), 'constant', constant_values=np.nan), axis=0)[df_zor.gorevli.unique()].to_excel('5_gorevli_bazinda_incelemede_dosya_listesi.xlsx', index=False)

You can use the following code:

to_csv = []

#to get 'a' and 'b' in first row
to_csv.append(list(result.keys()))

# To get 'a' and 'b' values
for a,b in  zip(*list(result.values())):
    to_csv.append([a,b])

# Writing result.csv
with open('result.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(to_csv)

The data in result.csv is:

a,b
1,3
2,6
3,1

You can use pandas

import pandas as pd
results_df = pd.DataFrame(data=result)
results_df.to_csv("result.csv", index=False)

Just add this at the end:

with open('output.csv', 'w') as fw:
    cw = csv.writer(fw, delimiter=',')
    row_list = list(result.keys()) + [x for x in zip(*result.values())]
    cw.writerows(row_list)

EDIT: I see my answer is a bit late. My answer is identical to @Mohnish answer except I use a "list comprehension" instead of explicitly looping through the zip object. @Mohnish answer has more explanation so go with that.

EDIT: Basically, instead of this:

# To get 'a' and 'b' values
for a,b in  zip(*list(result.values())):
    to_csv.append([a,b])

do this:

to_csv += [x for x in zip(*result.values())]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM