Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

group by list python

values = set(map(lambda x:x[1], mylist))
newlist = [[y[0] for y in mylist if y[1]==x] for x in values]
Comment

groupby and list

In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})
        df

Out[1]: 
   a  b
0  A  1
1  A  2
2  B  5
3  B  5
4  B  4
5  C  6

In [2]: df.groupby('a')['b'].apply(list)
Out[2]: 
a
A       [1, 2]
B    [5, 5, 4]
C          [6]
Name: b, dtype: object

In [3]: df1 = df.groupby('a')['b'].apply(list).reset_index(name='new')
        df1
Out[3]: 
   a        new
0  A     [1, 2]
1  B  [5, 5, 4]
2  C        [6]
Comment

group by pandas

#calculate sum of sales grouped by month
df.groupby(df.date.dt.month)['sales'].sum()

date
1    34
2    44
3    31
Name: sales, dtype: int64
Comment

pandas groupby

# usage example
gb = df.groupby(["col1", "col2"])
counts = gb.size().to_frame(name="counts")
count
(
    counts.join(gb.agg({"col3": "mean"}).rename(columns={"col3": "col3_mean"}))
    .join(gb.agg({"col4": "median"}).rename(columns={"col4": "col4_median"}))
    .join(gb.agg({"col4": "min"}).rename(columns={"col4": "col4_min"}))
    .reset_index()
)

# to create dataframe
keys = np.array(
    [
        ["A", "B"],
        ["A", "B"],
        ["A", "B"],
        ["A", "B"],
        ["C", "D"],
        ["C", "D"],
        ["C", "D"],
        ["E", "F"],
        ["E", "F"],
        ["G", "H"],
    ]
)



df = pd.DataFrame(
    np.hstack([keys, np.random.randn(10, 4).round(2)]), columns=["col1", "col2", "col3", "col4", "col5", "col6"]
)
df[["col3", "col4", "col5", "col6"]] = df[["col3", "col4", "col5", "col6"]].astype(float)
Comment

group by dataframe

df.groupby('A').agg({'B': ['min', 'max'], 'C': 'sum'})
Comment

groupby in python

# car_sales:> it is dataframe
# "Make" is column, or feature name
# operation is mean
car_sales.groupby(["Make"]).mean()
Comment

Pandas groupby

>>> emp.groupby(['dept', 'gender']).agg({'salary':'mean'}).round(-3)
Comment

pandas group by to dataframe

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]: 
      Name      City  City_Count  Name_Count
0    Alice   Seattle           1           1
1      Bob   Seattle           2           2
2  Mallory  Portland           2           2
3  Mallory   Seattle           1           1
Comment

python group by

df.groupby('group').assign(mean_var1 = lambda x: np.mean(x.var1)
Comment

PREVIOUS NEXT
Code Example
Python :: dataframe-name python 
Python :: add new column to pandas dataframe 
Python :: python permission denied 
Python :: installing python 3 to linux 
Python :: get chrome version with python 
Python :: pandas columns 
Python :: global python 
Python :: python conditional statement 
Python :: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. 
Python :: how to refresh page in flask 
Python :: how to debug python code in visual studio code 
Python :: migration django 
Python :: arithmetic operators in python 
Python :: how to run a python package from command line 
Python :: what is heapq in python 
Python :: sort pandas dataframe by specific column 
Python :: python elif syntax 
Python :: how to use prettify function in python 
Python :: tkinter while button not pressed 
Python :: how to create multiple dictionaries in python 
Python :: odd number sum in python 
Python :: flatten dict with lists as entries 
Python :: data type of none in python 
Python :: put grid behind matplotlib 
Python :: python anonymous object 
Python :: print("Hello world!") 
Python :: make guessing game by python 
Python :: rezise object pygame 
Python :: exchange sort python 
Python :: import .dat python 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =