Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas groupby values in list

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

Out: 
a
A       [1, 2]
B    [5, 5, 4]
C          [6]
Name: b, dtype: object
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

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 groupby to get list of values

groupByCol = "FID"
elementFromColToList = "name_ID"

temp = df.groupby(groupByCol)[elementFromColToList].apply(list).to_frame()
temp
                  
groupid      b      
1            [1, 2, 3]
3            [27]
4            [42, 42, 8]
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

PREVIOUS NEXT
Code Example
Python :: numpy mean 
Python :: Python Tkinter Text Widget Syntax 
Python :: multiprocessing join python 
Python :: python dict comprehension 
Python :: random.sample python 
Python :: python get array length 
Python :: Write Python programs to print numbers from 1 to 10000 while loops 
Python :: get number of key in dictionary python 
Python :: xpath start-with 
Python :: longest common subsequence python 
Python :: jupyter notebook not working 
Python :: custom attribute selenium 
Python :: online python 
Python :: bounding box python 
Python :: csv len python 
Python :: python text reverse 
Python :: filter dict by list of keys python 
Python :: python disable logging on unittest 
Python :: python flask how to remove last character from string 
Python :: how to pause a python script 
Python :: python discord 
Python :: python extract string 
Python :: import path in django 
Python :: pyautogui locatecenteronscreen mac fix 
Python :: run python script from repl 
Python :: static files not loading 404 error django 
Python :: python opencv measure distance two shapes 
Python :: str replace pandas 
Python :: add reaction discord.py 
Python :: how to make python code faster 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =