Pandas Groupby multiple columns with cumcount
Question:
I am new to python
I have a dataset where the same customer can apply for a product multiple times in a day and have fields for cust_number and date
when I apply
df['g']=dfc.groupby('CustNo','DATE').cumcount()
python errors
ValueError: No axis named DATE for object type DataFrame
is there an easy solution? I think an assignment of axis’?
help please
Answers:
The columns/groupers to use in groupby
should be passed as the first parameter. Here you pass two parameters, so the second is considered to be the axis
:
DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True,
group_keys=_NoDefault.no_default, squeeze=_NoDefault.no_default,
observed=False, dropna=True)
You need to wrap the column names in a list:
dfc.groupby(['CustNo', 'DATE']).cumcount()
check the column name in your data frame, whether "DATE" column is exist. if the date column is exist in ur data frame, first you need to change the date format, follow the below step.
import datetime
#use below syntax to convert your date column into date format
df[‘DATE’]=pd.to_datetime(df[‘DATE’])
I am new to python
I have a dataset where the same customer can apply for a product multiple times in a day and have fields for cust_number and date
when I apply
df['g']=dfc.groupby('CustNo','DATE').cumcount()
python errors
ValueError: No axis named DATE for object type DataFrame
is there an easy solution? I think an assignment of axis’?
help please
The columns/groupers to use in groupby
should be passed as the first parameter. Here you pass two parameters, so the second is considered to be the axis
:
DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True,
group_keys=_NoDefault.no_default, squeeze=_NoDefault.no_default,
observed=False, dropna=True)
You need to wrap the column names in a list:
dfc.groupby(['CustNo', 'DATE']).cumcount()
check the column name in your data frame, whether "DATE" column is exist. if the date column is exist in ur data frame, first you need to change the date format, follow the below step.
import datetime
#use below syntax to convert your date column into date format
df[‘DATE’]=pd.to_datetime(df[‘DATE’])