2018中國高校計算機大賽——大資料挑戰賽 --資料轉換為csv檔案格式
阿新 • • 發佈:2019-01-05
python3
# -*- coding: utf-8 -*-
# 載入pandas庫
import pandas as pd
# 實現資料集轉換。。。。原始資料集header都沒有。。。並且按id排序方便自己檢視
now_data = 'aline'
app_launch_log = pd.read_csv('app_launch_log.txt', sep='\t', header=None) # 將app_launch_log.txt讀取為Ò資料幀物件,加列索引,指定分隔符為\t
app_launch_log = app_launch_log.sort_index(by =[0, 1]) # 對資料幀排序,預設為從小到大,先按0排序後按1排序
app_launch_log = app_launch_log.rename({0: 'user_id', 1: 'launch_day'},
axis=1) # 修改資料幀的列名,0改為user_id,1改為launch_day,對行進行操作
app_launch_log.to_csv('handled_data/app_launch_log.csv', index=0) # 儲存資料幀到csv檔案,不保留行索引
user_activity_log = pd.read_csv('user_activity_log.txt' , sep='\t', header=None)
user_activity_log = user_activity_log.sort_index(by=[0, 1])
user_activity_log = user_activity_log.rename({0: 'user_id', 1: 'activity_day',
2: 'page', 3: 'video_id',
4: 'author_id', 5: 'action_type' }, axis=1)
user_activity_log.to_csv('handled_data/user_activity_log.csv', index=0)
user_register_log = pd.read_csv('user_register_log.txt', sep='\t', header=None)
user_register_log = user_register_log.sort_index(by=[0, 1])
user_register_log = user_register_log.rename({0: 'user_id', 1: 'register_day',
2: 'register', 3: 'device type'}, axis=1)
user_register_log.to_csv('handled_data/user_register_log.csv', index=0)
video_create_log = pd.read_csv('video_create_log.txt', sep='\t', header=None)
video_create_log = video_create_log.sort_index(by=[0, 1])
video_create_log = video_create_log.rename({0: 'user_id', 1: 'create_day'}, axis=1)
video_create_log.to_csv('handled_data/user_create_log.csv', index=0)