1

I am using group by apply and in the function in apply i am creating a csv. However, since i read somewhere that groupby is evaluated many times i am getting multiple csvs for the same data. How do i stop this from happening?

alerts = evening_data.groupby([identification, name, age,
                                         address]).apply(master, args='F').reset_index(drop=True)

def master(data, f):
    data.to_csv('test.csv')

I sometimes get two test.csv files. othertimes not.

CodeGeek123
  • 4,341
  • 8
  • 50
  • 79
  • dupe: https://stackoverflow.com/questions/21390035/python-pandas-groupby-object-apply-method-duplicates-first-group this is by design. What are you really trying to do here? You seem to be just sorting the df and then writing out the groups to csv. – EdChum Jun 22 '17 at 08:53
  • You could try to separate the definition of alters and then apply the function instead of concatenating both. – AaronDT Jun 22 '17 at 08:57
  • I am doing a lot more within these functions but i took them away so i can get to the problem – CodeGeek123 Jun 22 '17 at 09:08

1 Answers1

0

I believe is possible use:

for i, d in evening_data.groupby([identification, name, age,address]):
    master(d, 'F')

Also is nice change filenames by groups - something like:

def master(data, f):
    name = '{0[0]}{0[1]}{0[2]}{0[3]}'.format(data.index[0]) + '_test.csv'
    file_path = os.path.join(os.pardir, name)
    data.to_csv(file_path)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252