logo
Tags down

shadow

Predefining pandas aggregations and rename with new version


By : dpittle2112
Date : July 31 2020, 05:00 AM
it helps some times In previous versions of pandas, you could do: , An alternative is named agg:
code :
aggregations = {
    'SUM_name':('Col1','sum'),
    'MEAN_name':('Col1','mean'),
    'MEAN_newname':('Other_colname', 'mean')
}
agg_df = df.groupby(['gbcol']).agg(**aggregations)


Share : facebook icon twitter icon

Pandas - possible to aggregate two columns using two different aggregations?


By : JimmyTheMatrix
Date : March 29 2020, 07:55 AM
To fix the issue you can do The agg method can accept a dict, in which case the keys indicate the column to which the function is applied:
code :
grouped.agg({'numberA':'sum', 'numberB':'min'})
import numpy as np
import pandas as pd
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                   'number A': np.arange(8),
                   'number B': np.arange(8) * 2})
grouped = df.groupby('A')

print(grouped.agg({
    'number A': 'sum',
    'number B': 'min'}))
     number B  number A
A                      
bar         2         9
foo         0        19
print(df.columns)

Searching Multiple Strings in pandas without predefining number of strings to use


By : AL-BEN
Date : March 29 2020, 07:55 AM
Hope this helps I'm wondering if there's a more general way to do the below? I'm wondering if there's a way to create the st function so that I can search a non-predefined number of strings? , You could use np.logical_and.reduce:
code :
import pandas as pd
import numpy as np
def search(df, *words):  #1
    """
    Return a sub-DataFrame of those rows whose Name column match all the words.
    """
    return df[np.logical_and.reduce([df['Name'].str.contains(word) for word in words])]   # 2


df = pd.DataFrame({'Name':['Virginia Google Governor',
                           'Governor Virginia',
                           'Governor Virginia Google']})
print(search(df, 'Governor', 'Virginia', 'Google'))
                       Name
0  Virginia Google Governor
2  Governor Virginia Google

pandas add groupby aggregations


By : AstroPuls3
Date : March 29 2020, 07:55 AM
I hope this helps . I think you need concat + groupby + numpy.concatenate:
code :
df = pd.concat(n_items)
df = df.groupby(level=0).apply(lambda x: np.concatenate(x.values)).reset_index()
df = pd.concat([pd.read_pickle(file) for file in files], ignore_index=True)
df.timestamp = pd.to_datetime(df.timestamp)
df1 = df.groupby(pd.Grouper(key='timestamp', freq='1D')).items.unique()

I want to do some aggregations with the help of Group By function in pandas


By : user2899314
Date : March 29 2020, 07:55 AM
like below fixes the issue Start from defining a function to count vwap for the current month (group of rows):
code :
def vwap(grp):
    return (grp.price * grp.salesNo).sum() / grp.salesNo.sum()
df.groupby(df.dat.dt.to_period('M')).apply(vwap)
         dat  price  salesNo
0 2018-05-14  120.5       10
1 2018-05-16   80.0       22
2 2018-05-20   30.2       12
3 2018-08-10   75.1       41
4 2018-08-20   92.3       18
5 2019-05-10   10.0       33
6 2019-05-20   20.0       41
dat
2018-05    75.622727
2018-08    80.347458
2019-05    15.540541
Freq: M, dtype: float64

Combination aggregations in Pandas


By : gundabattula sriniva
Date : March 29 2020, 07:55 AM
Does that help I have data in this format , this should work:
shadow
Privacy Policy - Terms - Contact Us © voile276.org