pandas基礎分組

2021-10-03 07:31:35 字數 1854 閱讀 6215

,"outputs":[

],"source":[

"import numpy as np\n"

,"import pandas as pd"]}

,,"source":[

"### 分組 group"]}

,,"source":[

"#### 生成資料"]}

,,"outputs":[

,"execution_count":22

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df = pd.dataframe(data = )\n"

,"# 性別轉化\n"

,"def convert(x):\n"

," if x == 0:\n"

," return '女'\n"

," else:\n"

," return '男'\n",,

"# 左右手進行轉化\n"

,"def convert2(x):\n"

," if x < 19:\n"

," return 'right'\n"

," else:\n"

," return 'left'\n",,

"df"]}

,,"source":[

"#### 分組計算,前提,屬性相同"]}

,,"outputs":[

,"execution_count":18

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 用手習慣,進行分組,統計,iq和eq是否跟左右手影響\n"

,"# 分成兩組\n"

,"g = df.groupby(by = ['hand'])[['iq','eq']]\n"

,"g.max()"]}

,,"outputs":[

,"execution_count":20

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"g = df.groupby(by = ['hand'])[['iq','eq']]\n"

,"g.mean().round(1)"]}

,,"outputs":[

,"execution_count":21

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 分組的屬性可以是多個\n"

,"g = df.groupby(by = ['hand','***'])\n"

,"g.max()"]}

],"metadata":,

"language_info":,

"file_extension"

:".py"

,"mimetype"

:"text/x-python"

,"name"

:"python"

,"nbconvert_exporter"

:"python"

,"pygments_lexer"

:"ipython3"

,"version"

:"3.8.1"}}

,"nbformat":4

,"nbformat_minor":4

}

pandas 分組聚合

綜合使用 資料鏈結 統計每個國家的星巴克的數量 按照單字段聚合 codeing utf 8 import pandas as pd import numpy as np df pd.read csv data starbucks store worldwide.csv 按國家進行分組後會有很多列 然...

Pandas之資料分組

df.groupby by none axis 0,default 0 level none int,level name,or sequence of such,default none as index true bool,default true sort true group keys tr...

pandas分組運算(groupby)

按a列分組 groupby 獲取其他列的均值 方法1 b df b groupby df a mean 按a列分組,獲取b列的均值 print b 方法2 b df.ix 1 groupby df.ix 0 mean 按a列分組 0對應a列,1對應b列 獲取b列的均值 print b 方法3 2.聚...