this is the equivalent of my csv file;
JavaScript
x
14
14
1
customer,quantity
2
a,250
3
a,166
4
c,354
5
b,185
6
a,58
7
d,68
8
c,263
9
c,254
10
d,320
11
b,176
12
d,127
13
14
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
JavaScript
1
19
19
1
# pip install pandas
2
import pandas as pd
3
4
data = pd.read_csv('abc.csv') # write you csv file name here.
5
6
all_customer_value = list(data['customer'])
7
all_customer_quantity = list(data['quantity'])
8
all_customer_name = set(data['customer'])
9
10
for user in all_customer_name:
11
with open(f'{user}.csv','w')as file:
12
file.write('customer,quantityn') # edited
13
14
for index,value in enumerate(all_customer_value):
15
with open(f'{value}.csv','a') as file:
16
file.write(f'{value}, {all_customer_quantity[index]}n')
17
18
19