How to do a conditional count after groupby on a Pandas Dataframe?

I think you need add condition first: #if need also category c with no values of ‘one’ df11=df.groupby(‘key1’)[‘key2′].apply(lambda x: (x==’one’).sum()).reset_index(name=”count”) print (df11) key1 count 0 a 2 1 b 1 2 c 0 Or use categorical with key1, then missing value is added by size: df[‘key1’] = df[‘key1’].astype(‘category’) df1 = df[df[‘key2’] == ‘one’].groupby([‘key1’]).size().reset_index(name=”count”) print (df1) … Read more

Use pandas.shift() within a group

Pandas’ grouped objects have a groupby.DataFrameGroupBy.shift method, which will shift a specified column in each group n periods, just like the regular dataframe’s shift method: df[‘prev_value’] = df.groupby(‘object’)[‘value’].shift() For the following example dataframe: print(df) object period value 0 1 1 24 1 1 2 67 2 1 4 89 3 2 4 5 4 2 … Read more

Python Pandas Group by date using datetime data

You can use groupby by dates of column Date_Time by dt.date: df = df.groupby([df[‘Date_Time’].dt.date]).mean() Sample: df = pd.DataFrame({‘Date_Time’: pd.date_range(’10/1/2001 10:00:00′, periods=3, freq=’10H’), ‘B’:[4,5,6]}) print (df) B Date_Time 0 4 2001-10-01 10:00:00 1 5 2001-10-01 20:00:00 2 6 2001-10-02 06:00:00 print (df[‘Date_Time’].dt.date) 0 2001-10-01 1 2001-10-01 2 2001-10-02 Name: Date_Time, dtype: object df = df.groupby([df[‘Date_Time’].dt.date])[‘B’].mean() print(df) … Read more

Renaming Column Names in Pandas Groupby function [duplicate]

For the first question I think answer would be: <your DataFrame>.rename(columns={‘count’:’Total_Numbers’}) or <your DataFrame>.columns = [‘ID’, ‘Region’, ‘Total_Numbers’] As for second one I’d say the answer would be no. It’s possible to use it like ‘df.ID’ because of python datamodel: Attribute references are translated to lookups in this dictionary, e.g., m.x is equivalent to m.dict[“x”]

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)