Q: How to get the most common record per ID in DataFrame I have a DataFrame with 20k records and 650 unique IDs. I want to obtain the most common record for each ID using mode().
Data:
 ID Device Country Region City
100    Win       A      X    P
100    Win       A      X    P
100    Win       A      X    P
200    Mac       B      Y    Q
200    Mac       B      Y    Q
300  Linux       C      Z    R
300  Linux       C      Z    R

Desired outcome:
 ID Device Country Region City
100    Win       A      X    P
200    Mac       B      Y    Q
300  Linux       C      Z    R

I have written this code to get the desired result, but it gives zero records:
for id in df.groupby('id'):
    df_new = pd.DataFrame() #creating blank dataframe 
    df_new['Device'] = df['Device'].mode()[0]
    df_new['Country'] = df['Country'].mode()[0]
    df_new['Region'] = df['Region'].mode()[0]
    df_new['City'] = df['City'].mode()[0]


A: This seems to do what you want:
df.groupby("ID").apply(pd.DataFrame.mode).reset_index(drop=True)

Output:
    ID Device Country Region City
0  100    Win       A      X    P
1  200    Mac       B      Y    Q
2  300  Linux       C      Z    R

