Output Groupby To Csv File Pandas
I have a sample dataset: import pandas as pd df = {'ID': ['H1','H2','H3','H4','H5','H6'], 'AA1': ['C','B','B','X','G','G'], 'AA2': ['W','K','K','A','B','B'], 'na
Solution 1:
The problem is that you are trying to apply a function to_csv
which doesn't exist. Anyway, groupby also doesn't have a to_csv method. pd.Series
and pd.DataFrame
do.
What you should really use is drop_duplicates
here and then export the resulting dataframe to csv:
df.drop_duplicates(['AA1','AA2']).to_csv('merged.txt')
PS: If you really wanted a groupby solution, there's this one that happens to be 12 times slower than drop_duplicates...:
df.groupby(['AA1','AA2']).agg(lambda x:x.value_counts().index[0]).to_csv('merged.txt')
Solution 2:
you can use groupby
with head
df.groupby(['AA1', 'AA2']).head(1)
Post a Comment for "Output Groupby To Csv File Pandas"