0%

pandas 数据处理02 groupby

pandas 数据处理02 聚合分组

时间戳 转换时间

1
2
3
4
5
6
7
8
9
10
import pandas as pd
import numpy as np

df = pd.read_csv("./train.csv")

print(df)
d_time = pd.to_datetime(df['time'], unit='s')
print(type(d_time)) # <class 'pandas.core.series.Series'>
print('--------------')
print(d_time)

时间值 处理

1609116502837

删除 缺失值 dropna

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pandas as pd
import numpy as np

df = pd.read_csv('./breast-cancer-wisconsin.data')

df = df.replace(to_replace='?', value=np.nan)

print(df)
print('--------------')
# 默认是0 删除行数据 为1 删除列数据
# how='all' 全部是nan 删除
# how='any' 有nan就删除

df1 = df.dropna(axis=0, how='all')
print(df1)

pandas的合并

1
2
3
4
5
6
7
8
9
import pandas as pd
import numpy as np

df_data = pd.read_csv('./directory.csv')
print(df)
# 指定列的 合并
pd.merge()


merge 案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pandas as pd
import numpy as np

# 商品详情 product_id 商品的名字 和 aisle_id
product = pd.read_csv("C:/Users/halon/Desktop/数据挖掘/day05/instacart/products.csv")
# orderid product_id
order_pro = pd.read_csv("C:/Users/halon/Desktop/数据挖掘/day05/instacart/order_products__prior.csv")

# orderId userId
orders = pd.read_csv("C:/Users/halon/Desktop/数据挖掘/day05/instacart/orders.csv")

# aisles_nameid aislesId
aisles = pd.read_csv("C:/Users/halon/Desktop/数据挖掘/day05/instacart/aisles.csv")

# 合并product和order_pro, 通过product_id
mg1 = pd.merge(order_pro, product, on=['product_id'])

# 合并mg1 和orders 通过order_id
mg2 = pd.merge(mg1, orders, on=['order_id'])

# 合并mg2 和aisles 通过aisle_id
mg3 = pd.merge(mg2, aisles, on=['aisle_id'])

# 用户购买的所有类商品的数量
user_ai = pd.crosstab(mg3['user_id'],mg3['aisle'])
print(user_ai)

聚合分组1

1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd
import numpy as np

df_data = pd.read_csv('./directory.csv')
print(df)
# 按国家分组
df_country = df.groupby('Country')
print(df_country.count().reset_index())
# 按国家 和省分组
df_country_Province = df.gourpby(['Country','State/Province'])

# 查看数据
print(df_country_Province.count().reset_index())

聚合函数+分组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pandas as pd


df = pd.DataFrame({
'color': ['white', 'red', 'green', 'red', 'green'],
'object': ['pen', 'pencil', 'pencil', 'ashtray', 'pen'],
'price1': [5.56, 4.20, 1.30, 0.56, 2.75],
'price2': [4.75, 4.12, 1.60, 0.75, 3.15]
})

print(df)

# 先分组, 后聚合
df_color = df.groupby('color')
print(type(df_color))
print(df_color.mean())
print(df_color.max())
print('---------------------------------')
# 第二种 先聚合 再分组 (不常用)
price_m = df['price2'].groupby(df['color']).mean()
print(price_m)

------ 本文结束------

欢迎关注我的其它发布渠道