Separate repeated values from a column in csv Python

1

I'm wanting to work with a CSV in Python.

ID           Inventory    Domain                   Requests   Impressions      Fill Rate
123456       auto         neighborhoodscout.com      11402        26            0.23
123456       auto         sinembargo.mx              10334        24            0.23
123456       auto         elsalvadortimes.com        9893         17            0.17
155444       camioneta    thealternativedaily.com    51389        81            0.16
155444       camioneta    heywise.com                45578       135            0.3
155444       camioneta    wis.pr                     28792        69            0.24

These are my CSV values. I need to create a CSV for each inventory field. Basically it would be a csv with the 3 auto data and another with the 3 of the truck in this case. I'm pretty new to Python and read and extract data from a csv the problem is when I have to do what I ask earlier! It is worth clarifying that if or if I have to work reading the CSV that I have with all the data.

Thank you very much already.

Greetings

    
asked by Martin Bouhier 03.10.2017 в 23:30
source

1 answer

1

To create a csv for each group in your first column you can use pandas.groupby to create the groups and then save each one in a csv with the name of the group with pandas.to_csv :

import pandas as pd

df = pd.read_csv("datos.csv",  header=0,  sep = ",")
for group in df.groupby(df["Inventory"]):
    group[1].to_csv("{}.csv".format(group[0]), sep=',', index=False)

Assuming that your csv was called "datos.csv" we would get two files:

auto.csv:

Inventory                  Domain   Requests    Impressions  Fill Rate
auto        neighborhoodscout.com      11402             26       0.23
auto                sinembargo.mx      10334             24       0.23
auto          elsalvadortimes.com       9893             17       0.17

truck.csv:

Inventory                  Domain   Requests    Impressions  Fill Rate
camioneta thealternativedaily.com      51389             81       0.16
camioneta             heywise.com      45578            135        0.3
camioneta                  wis.pr      28792             69       0.24

No matter what groups exist, each one will create a csv with the name of the group automatically.

Each group returned by groupby is a tuple where the first element is the identifier of the group and the second the DataFrame that corresponds to that group.

    
answered by 04.10.2017 / 00:16
source