More Related Content Similar to Pandas presentation (20) Pandas presentation5. その他のデータの読み込みその他のデータの読み込み
import pandas
items = pandas.read_csv('items.csv')
print(items.head())
itemid itemname itemprice
0 I001 MediaPlayer 50000
1 I002 SmartPhone 85000
2 I003 LaptopPC 120000
3 I004 DesktopPC 180000
4 I005 GamingPC 210000
4
6. import pandas
log_1 = pandas.read_csv('log_1.csv')
print(log_1.head())
logid price paymentdate
1 T0000000114 50000 2019-02-01 01
2 T0000000115 120000 2019-02-01 02
3 T0000000116 210000 2019-02-01 02
4 T0000000117 170000 2019-02-01 04
5
7. import pandas
log_detail_1 = pandas.read_csv('log_detail_1.csv')
print(log_detail_1.head())
detailid logid itemid quantity
0 0 T0000000113 I005 1
1 1 T0000000114 I001 1
2 2 T0000000115 I003 1
3 3 T0000000116 I005 1
4 4 T0000000117 I002 2
6
8. データ件数の出力データ件数の出力
# -*- coding: utf-8 -*-
import pandas
customers = pandas.read_csv('customers.csv') # 顧客データ
items = pandas.read_csv('items.csv') # 商品データ
log_1 = pandas.read_csv('log_1.csv') # 購入データ
log_detail_1 = pandas.read_csv('log_detail_1.csv') # 購入詳細データ
print('顧客データ:', len(customers),'件')
print('商品データ:', len(items), '件')
print('購入データ:', len(log_1), '件')
print('購入詳細データ:', len(log_detail_1), '件')
顧客データ: 5000 件
商品データ: 5 件
購入データ: 5000 件
10. 前処理前処理
購入データと購入詳細データを結合する. 分割されている
購入データ1と2を concat関数を利用して結合する. この
ように縦方向にデータを結合することをユニオンと言う.
# -*- coding: utf-8 -*-
import pandas
log_1 = pandas.read_csv('log_1.csv')
log_2 = pandas.read_csv('log_2.csv')
logs = pandas.concat([log_1, log_2], ignore_index=True)
print(f'{len(log_1)} + {len(log_2)} ={len(logs)}')
log_detail_1= pandas.read_csv('log_detail_1.csv')
log_detail_2= pandas.read_csv('log_detail_2.csv')
# ignore_index=Trueとするとindexを0から振りなおしてくれる.
log_details= pandas.concat([log_detail_1,log_detail_2], ignore_index=True)
print(f'{len(log_detail_1)} + {len(log_detail_2)} ={len(log_details)}')
12. 購入データと購入詳細デー購入データと購入詳細デー
タを結合するタを結合する
import pandas
log_1 = pandas.read_csv('log_1.csv')
log_2 = pandas.read_csv('log_2.csv')
logs = pandas.concat([log_1, log_2], ignore_index=True)
log_detail_1= pandas.read_csv('log_detail_1.csv')
log_detail_2= pandas.read_csv('log_detail_2.csv')
log_details= pandas.concat([log_detail_1,log_detail_2], ignore_index=True)
merge_data = pandas.merge(
log_details,
logs[['log_id' , 'payment_date', 'customer_id']],
on='log_id' ,
how= 'left')
print(merge_data.head())
9
15. detailid logid itemid customer
0 0 T0000000113 I005 PL563502
1 1 T0000000114 I001 HD67801
2 2 T0000000115 I003 HD29812
3 3 T0000000116 I005 IK452215
4 4 T0000000117 I002 PL542865
12
17. detailid logid itemid customer
0 0 T0000000113 I005 PL563502
1 1 T0000000114 I001 HD67801
2 2 T0000000115 I003 HD29812
3 3 T0000000116 I005 IK452215
4 4 T0000000117 I002 PL542865
14
32. 201903 DesktopPC 25740000 143
GamingPC 64050000 305
LaptopPC 19080000 159
MediaPlayer 26000000 520
SmartPhone 25500000 300
201904 DesktopPC 24300000 135
GamingPC 64890000 309
LaptopPC 21960000 183
MediaPlayer 25900000 518
SmartPhone 23460000 276
33. 201905 DesktopPC 25920000 144
GamingPC 58800000 280
LaptopPC 20520000 171
MediaPlayer 24850000 497
SmartPhone 25330000 298
201906 DesktopPC 28800000 160
GamingPC 63420000 302
LaptopPC 21840000 182
MediaPlayer 26000000 520
SmartPhone 23970000 282
34. 201907 DesktopPC 26100000 145
GamingPC 71610000 341
LaptopPC 19440000 162
MediaPlayer 25250000 505
SmartPhone 28220000 332
21
39. 折れ線グラフ折れ線グラフ
import matplotlib.pyplot as plt
for item_name in graph_data.keys():
plt.plot(graph_data[item_name].index, graph_data[item_name].values,label=item_name)
plt.legend()
plt.savefig('images/oresen.png')
plt.show()