NData
NData

Reputation: 59

Make CSV file from each row

I have a dataframe with n columns. One is the city name.

I am trying to extract each row(s) based on city name and print each to a new CSV file.

col1  col2    cityName

a       5     Madrid
s       10    NEWYORK
d       0     London
f       2     Berlin

Answer:

first csv file:

col1  col2    cityName

a       5     Madrid

second csv file:

col1  col2    cityName
s       10    NEWYORK

Upvotes: 0

Views: 3692

Answers (3)

Denis Yakovlev
Denis Yakovlev

Reputation: 497

You can group rows by cityName column and then export to separate csv every group like this:

EDIT: add save results to csv files

EDIT: add zip all files

import zipfile
import pandas as pd

df = pd.DataFrame(
    data={'col1': ['a', 's', 'd', 'f', 'x'], 'col2': [5, 10, 0, 2, 4],
          'cityName': ['Madrid', 'NEWYORK', 'London', 'Berlin', 'Madrid']})

groupby = df.groupby('cityName')
out_zip = zipfile.ZipFile("58377674.zip", "w")

for n, g in groupby:
    print(n)
    csv = g.to_csv(index=False)
    print(csv)
    csv_filename = "{}.csv".format(n)
    with open(csv_filename, 'w') as out_file:
        out_file.write(csv)
    out_zip.write(csv_filename)

Upvotes: 1

Ravinder Saluja
Ravinder Saluja

Reputation: 40

This code will iterate over any DataFrame(if exists) and create a new csv file(for every row) containing the column header as well as the row or else if the DataFrame doesn't exist it will not do anything.

import pandas as pd
i=0
while True:
    try: 
        df.iloc[[i]].to_csv("filename.csv")
        i = i+1
    except: pass

Upvotes: 1

Daniyal Tariq
Daniyal Tariq

Reputation: 201

The Following code will make a CSV file, and write a row in that. You can iterate over this code till your last record, and each time it will make a unique data file.

import csv
from datetime import datetime

for index, row in dataframe.iterrows():
    filename = datetime.now()
    filename = filename.strftime("%d-%m-%Y_%H:%M:%S")
    file = open('data_'+str(filename)+'.csv', 'w')
    w = csv.writer(file)
    w.writerow([row['col1'], row['col2'], row['cityName']])
    file.close()

Upvotes: -1

Related Questions