I have a combined table for multiple IDs.
| ID | Type | Color | Size | Quantity |
|---|---|---|---|---|
| 1 | A | Wht | S | 7 |
| 1 | B | Ylw | L | 9 |
| 2 | A | Grn | S | 2 |
| 2 | C | Wht | M | 7 |
| 3 | B | Blk | M | 3 |
For each unique ID, I would like to write a text file, named <ID>.txt.
Within each file, I would like to write the Type and Color values. For example:
1.txt
AWht
BYlw
Here's what I have so far, but I am stuck on two things:
- Insert ID values as the file names. It seems to only accept text strings.
- Insert Type and Color values in the written contents of each file.
import pandas as pd
data = [['1','A','Wht','S','7'],['1','B','Ylw','L','9'],['2','A','Grn','S','2'],['2','C','Wht','M','7'],['3','B','Blk','M','3']]
df = pd.DataFrame(data, columns = ['ID','Type','Color','Size','Quantity'])
IDs = df.ID.unique()
IDs.sort()
for i in IDs:
with open('filename.txt', 'w') as f:
f.write('TypeColor')