'Pandas Apply Exception
I am trying to build a function to give me the most common value in a column and when there is none just skip it or apply np.nan. However .idmax() causes issues on some columns where it is int64 or a string. Any easy way to generate most common value for a column? thanks
def df_missing_info(df):
most_common_value = df.apply(lambda x: x.value_counts().idxmax())
Solution 1:[1]
Because Series.value_counts also sorting Series get first index for maximal counts and add next with iter for return first value if exist else get default value, here NaN:
print (df)
id pts x y
0 1 NaN a NaN
1 1 NaN a NaN
2 1 NaN s NaN
3 2 0.1 a NaN
def df_missing_info(df):
return df.apply(lambda x: next(iter(x.value_counts().index), np.nan))
a = df_missing_info(df)
print (a)
id 1
pts 0.1
x a
y NaN
dtype: object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
