This questions was a little harder for me to phrase so I request to help edit the question which would make more sense (if necessary).
Problem Statement: I want all the rows which have a specific column value in common, saved to same file.
Example Code I want to do something like this. Say, I have a dataframe:
d = {'col1': [1, 2, 6, 3, 4], 'col2': [3, 4, 2, 5, 6], 'col3':['a', 'b', 'c', 'a', 'b'], 'col4':['2', '3', '2', '2', '2']}
df = pd.DataFrame(data=d)
I want to create csv files such that:
- all rows where
col3
isa
, they all get saved ina.csv
all rows - where
col3
isb
, they all get saved inb.csv
all rows where col3
isc
, they all get saved inc.csv
Hypothesized Solution: The only way I can think of creating the CSV files is iterating through the dataframe per row and checking if the column (e.g. col3
val) has a csv created already, if not -- create and add the rows or else append to exists csv file.
Issue:
Above sample code is just a representation. I have a very large dataframe. If it helps, I know the unique value in the column in question (like, col3
is example) as a list somewhere. However, on of the most popular answer on how to iterate over a dataframe?
: How to iterate over rows in a DataFrame in Pandas says (in the second answer there) that DON'T
. I might have to use it as a last resort if there is no other way but if there is one, can someone help me get a better solution to this problem?