How can I save csv separately based on it's entry column
Pandas is great at selecting only relevant informations. You have simply to do:
for each_id in id_list:
item = int(each_id)
data[data[id] == item].to_csv(os.path.join(dir, f'test_id_{item}.csv'), sep=',' )
It could be:
...
data[data[id] == each_id].to_csv(os.path.join(dir, f'test_id_{item}.csv'), sep=',' )
if the id
column was not numeric...
But if you have a large dataframe and a rather high number of id values, it would not be efficient because you will repeat the extraction process for each id value. The Pandas way is to use grouby
:
for item, df_item in df.groupby('id'):
df_item.to_csv(os.path.join(dir, f'test_id_{item}.csv'), sep=',' )
For each id you need to split out a set of data from the main dataframe and save that separately.
To do that you can use something like this.
for each_id in id_list:
item = int(each_id)
data_for_id = data[data['id.'] == item]
data_for_id.to_csv(os.path.join(dir, f'test_id_{item}.csv'), sep=',' )