Skip to content
Advertisement

I would like to take a data from in the csv file

this is the equivalent of my csv file;

customer,quantity
a,250
a,166
c,354
b,185
a,58
d,68
c,263
c,254
d,320
b,176
d,127
...

this csv file has 8000 data. I want to separate the “a”, “b”, ,”c”, … “z” in the customer column with the quantity column. this csv file is just an example, actually customers are too many. I don’t know the customer names. what i want is for each client to have their own csv file. and I have to do them using python.

I’m sorry for my bad english.

Advertisement

Answer

I am not good in the pandas module but I get what you want this code makes the .csv of the user name and the inserts his/her name and quantity in the file. You can try this if you get any errors then please let me know in the comment. Note: Please try this with a copy of the same data file

# pip install pandas
import pandas as pd

data = pd.read_csv('abc.csv') # write you csv file name here.

all_customer_value = list(data['customer'])
all_customer_quantity = list(data['quantity'])
all_customer_name = set(data['customer'])

for user in all_customer_name:
    with open(f'{user}.csv','w')as file:
        file.write('customer,quantityn') # edited

for index,value in  enumerate(all_customer_value):
    with open(f'{value}.csv','a') as file:
        file.write(f'{value}, {all_customer_quantity[index]}n')


User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement