I have a small sample dataset:
import pandas as pd
df = {'ID': ['H576','H577','H577','H578','H600', 'H700', 'H700'],
  'CD': ['AAAAAAA', 'BBBBB', 'CCCCCC','DDDDDD', 'EEEEEEE','FFFFFFF','GGGGGGG']}
df = pd.DataFrame(df)
it looks like:
df
Out[9]: 
        CD    ID
0  AAAAAAA  H576
1    BBBBB  H577
2   CCCCCC  H577
3   DDDDDD  H578
4  EEEEEEE  H600
5  FFFFFFF  H700
6  GGGGGGG  H700
For each ID that has more than one CD values, i want to save them to a separate file.
my desire output files:
H577.txt
  CD      ID
 BBBBB   H577
 CCCCCC  H577
H700.txt
  CD       ID
 FFFFFFF  H700
 GGGGGGG  H700
my attempt:
import pandas as pd
df = {'ID': ['H576','H577','H577','H578','H600', 'H700', 'H700'],
  'CD': ['AAAAAAA', 'BBBBB', 'CCCCCC','DDDDDD', 'EEEEEEE','FFFFFFF','GGGGGGG']}
df = pd.DataFrame(df)
df1 = (df.groupby('ID').filter(lambda x: ('if CD has more than one value for the same ID'.any())))
df1.groupby('ID').apply(lambda gp: gp.to_csv('ID{}.txt'.format(gp.name), sep='\t', index=False))
i am not sure how to code the 'if CD has more than one value of the same ID' part.
 
    