Search code examples
pythonpandasconcatenationmultiple-columnsnested-loops

Multiple texts output according to multiple values in 2 columns


I am starting to code in python (Pandas library) because of my work. Here is an example with pokemons but my real dataframe is more complex. I dont know if it's possible to do it in Pandas.

My dataframe is like:

   Pokemon_Master      Pokemon 
1   ASH                pikachu
2   ASH                squirtle
3   ASH                charmander
4   BROOKE             onix
5   BROOKE             charmander
6   MISTY              squirtle
7   MISTY              charmander
8   GARY               onix 
9   PROF OAK           pikachu

What I have to do is: to check which Pokemon_Master each Pokemon belongs too and make a .txt output for each Pokemon_Master in the following format: Ash_pokemons.txt or Brooke_pokemons.txt, etc. Each file has to contain the pokemons which each Master has, e.g. each .txt file will concatenate progressively with the previous one as they are discovering pokemons in the dataframe.
Also the output text file will be called based on the Pokemon_Master. For example: Inside the file ASH.txt

pikachu 
squirtle
charmander

and Inside the file BROOKE.txt

onix 
charmander  
squirtle

etc..

I have discovered lots of basic functions like loc, iterrows concatenate or append (for joining files), but I cannot find the way how to join all of this to develop my code.


Solution

  • IIUC, try:

    for n, g in df.groupby('Pokemon_Master'):
        g[['Pokemon']].agg(' '.join).to_csv(f'{n}.txt', sep=' ', 
                                            index=False, 
                                            header=False)
    

    If Python version less than 3.6:

    Try,

    for n, g in df.groupby('Pokemon_Master'):
        g[['Pokemon']].agg(' '.join).to_csv('{}.txt'.format(n), sep=' ', 
                                            index=False, 
                                            header=False)