I want my function to count how many nans/nones appear in the column as well as whatever numbers or strings etc.
This differs from the using count vs size in the group by as the size
doesn't give the number of occurrences of NaNs it just shows NaN at each row for those. Count does the same - open to changing how the function gets the count of occurrences as long as i am able to get desired output. Thanks in advance!!
This is the function:
def count_how_many(df: pd.DataFrame,in_col, out_col, inplace=False):
def count_occurrences(dff: pd.DataFrame) -> pd.DataFrame:
return dff.groupby(in_col)[in_col].transform('count')
if not inplace:
copy_df = df.copy()
copy_df[out_col] = count_occurrences(copy_df)
return copy_df
else:
df[out_col] = count_occurrences(df)
return None
My df1 looks like this:
number |
---|
1 |
2 |
3 |
1 |
np.nan |
np.nan |
4 |
And I want count_how_many(df1, "number", "count")
to look like this:
number | count |
---|---|
1 | 2 |
2 | 1 |
3 | 1 |
1 | 2 |
np.nan | 2 |
np.nan | 2 |
4 | 1 |
i need this function to work for strings as well df2:
item |
---|
cookies |
cake |
brownie |
cake |
cake |
brownie |
And I want count_how_many(df2, "item", "count")
to look like this:
item | count |
---|---|
cookies | 1 |
cake | 3 |
brownie | 2 |
cake | 3 |
cake | 3 |
brownie | 2 |