测试数据:
context_id subject_gmt differtime browse_count click_count like_count commet_count reply_count score_value
last1 2018/8/12 10:35 2920.066 150 78 55 15 46 5
last2 2018/8/11 15:02 4093.066 68 57 42 6 10 4
last3 2018/8/13 15:10 1205.066 54 12 5 0 10 4
last4 2018/7/13 10:10 46145.066 853 833 556 358 665 8
last5 2018/8/12 9:35 2980.066 12 5 1 0 7 2
last6 2018/8/13 14:02 1273.066 8 2 0 2 6 2
last7 2018/8/10 15:10 5525.066 812 765 589 652 234 12
last8 2018/7/13 9:10 46205.066 87 82 65 79 86 3
last9 2018/8/12 13:05 2770.066 189 135 102 132 84 4
last10 2018/8/11 15:55 4040.066 335 226 188 220 175 8
我们常常需要将DataFrame对象中的某列或某几列作为索引,或者将索引转化为对象的列。
pandas提供了set_index()/reset_index() 来供我们使用。
# -*- encoding=utf-8 -*-
import pandas as pd
ceshi = pd.read_csv(u'F:/下载/context_data.csv')
# print(ceshi)
# melt行转列
bd = pd.melt(ceshi, id_vars=['context_id','subject_gmt'],value_vars=['browse_count','click_count','like_count','commet_count','reply_count','score_value'], var_name='count_type',value_name='count_value')
# print(bd)
# pivot_table 列转行
mdd = pd.DataFrame(pd.pivot_table(bd, index = ['context_id','subject_gmt'],columns = 'count_type', values = 'count_value'))
# 索引转换为列
mdd.reset_index(inplace=True)
print(mdd)
E:\laidefa\python.exe "E:/Program Files/pycharmproject/线上模型/行转列.py"
context_id subject_gmt count_type count_value
0 last1 2018/8/12 10:35 browse_count 150
1 last2 2018/8/11 15:02 browse_count 68
2 last3 2018/8/13 15:10 browse_count 54
3 last4 2018/7/13 10:10 browse_count 853
4 last5 2018/8/12 9:35 browse_count 12
5 last6 2018/8/13 14:02 browse_count 8
6 last7 2018/8/10 15:10 browse_count 812
7 last8 2018/7/13 9:10 browse_count 87
8 last9 2018/8/12 13:05 browse_count 189
9 last10 2018/8/11 15:55 browse_count 335
10 last1 2018/8/12 10:35 click_count 78
11 last2 2018/8/11 15:02 click_count 57
12 last3 2018/8/13 15:10 click_count 12
13 last4 2018/7/13 10:10 click_count 833
14 last5 2018/8/12 9:35 click_count 5
15 last6 2018/8/13 14:02 click_count 2
16 last7 2018/8/10 15:10 click_count 765
17 last8 2018/7/13 9:10 click_count 82
18 last9 2018/8/12 13:05 click_count 135
19 last10 2018/8/11 15:55 click_count 226
20 last1 2018/8/12 10:35 like_count 55
21 last2 2018/8/11 15:02 like_count 42
22 last3 2018/8/13 15:10 like_count 5
23 last4 2018/7/13 10:10 like_count 556
24 last5 2018/8/12 9:35 like_count 1
25 last6 2018/8/13 14:02 like_count 0
26 last7 2018/8/10 15:10 like_count 589
27 last8 2018/7/13 9:10 like_count 65
28 last9 2018/8/12 13:05 like_count 102
29 last10 2018/8/11 15:55 like_count 188
30 last1 2018/8/12 10:35 commet_count 15
31 last2 2018/8/11 15:02 commet_count 6
32 last3 2018/8/13 15:10 commet_count 0
33 last4 2018/7/13 10:10 commet_count 358
34 last5 2018/8/12 9:35 commet_count 0
35 last6 2018/8/13 14:02 commet_count 2
36 last7 2018/8/10 15:10 commet_count 652
37 last8 2018/7/13 9:10 commet_count 79
38 last9 2018/8/12 13:05 commet_count 132
39 last10 2018/8/11 15:55 commet_count 220
40 last1 2018/8/12 10:35 reply_count 46
41 last2 2018/8/11 15:02 reply_count 10
42 last3 2018/8/13 15:10 reply_count 10
43 last4 2018/7/13 10:10 reply_count 665
44 last5 2018/8/12 9:35 reply_count 7
45 last6 2018/8/13 14:02 reply_count 6
46 last7 2018/8/10 15:10 reply_count 234
47 last8 2018/7/13 9:10 reply_count 86
48 last9 2018/8/12 13:05 reply_count 84
49 last10 2018/8/11 15:55 reply_count 175
50 last1 2018/8/12 10:35 score_value 5
51 last2 2018/8/11 15:02 score_value 4
52 last3 2018/8/13 15:10 score_value 4
53 last4 2018/7/13 10:10 score_value 8
54 last5 2018/8/12 9:35 score_value 2
55 last6 2018/8/13 14:02 score_value 2
56 last7 2018/8/10 15:10 score_value 12
57 last8 2018/7/13 9:10 score_value 3
58 last9 2018/8/12 13:05 score_value 4
59 last10 2018/8/11 15:55 score_value 8
count_type context_id subject_gmt browse_count click_count \
0 last1 2018/8/12 10:35 150 78
1 last10 2018/8/11 15:55 335 226
2 last2 2018/8/11 15:02 68 57
3 last3 2018/8/13 15:10 54 12
4 last4 2018/7/13 10:10 853 833
5 last5 2018/8/12 9:35 12 5
6 last6 2018/8/13 14:02 8 2
7 last7 2018/8/10 15:10 812 765
8 last8 2018/7/13 9:10 87 82
9 last9 2018/8/12 13:05 189 135
count_type commet_count like_count reply_count score_value
0 15 55 46 5
1 220 188 175 8
2 6 42 10 4
3 0 5 10 4
4 358 556 665 8
5 0 1 7 2
6 2 0 6 2
7 652 589 234 12
8 79 65 86 3
9 132 102 84 4
Process finished with exit code 0