temp_dic = {'a':1, 'b':2, 'c':3} # 인덱스는 숫자나 문자나 모두 가능하다. ser = pd.Series(temp_dic) print(ser) print(type(ser))
a 1
b 2
c 3
dtype: int64
<class 'pandas.core.series.Series'>
언뜻 보기에는 같아보인다.
그러나, 다른 클래스고, 메서드도 다르다. 조심해야 한다.
데이터 불러오기
구글 드라이브 연동
Lemonade2016.csv 파일
1 2
from google.colab import drive drive.mount('/content/drive')
Mounted at /content/drive
1 2 3
DATA_PATH = '/content/drive/MyDrive/Colab Notebooks/data/Lemonade2016.csv' juice = pd.read_csv(DATA_PATH) # 객체를 대문자로 썼는데, 개발자들이 좋아하는 방법이다. 눈에 띄는 걸 좋아한다. juice
Date
Location
Lemon
Orange
Temperature
Leaflets
Price
0
7/1/2016
Park
97
67
70
90.0
0.25
1
7/2/2016
Park
98
67
72
90.0
0.25
2
7/3/2016
Park
110
77
71
104.0
0.25
3
7/4/2016
Beach
134
99
76
98.0
0.25
4
7/5/2016
Beach
159
118
78
135.0
0.25
5
7/6/2016
Beach
103
69
82
90.0
0.25
6
7/6/2016
Beach
103
69
82
90.0
0.25
7
7/7/2016
Beach
143
101
81
135.0
0.25
8
NaN
Beach
123
86
82
113.0
0.25
9
7/9/2016
Beach
134
95
80
126.0
0.25
10
7/10/2016
Beach
140
98
82
131.0
0.25
11
7/11/2016
Beach
162
120
83
135.0
0.25
12
7/12/2016
Beach
130
95
84
99.0
0.25
13
7/13/2016
Beach
109
75
77
99.0
0.25
14
7/14/2016
Beach
122
85
78
113.0
0.25
15
7/15/2016
Beach
98
62
75
108.0
0.50
16
7/16/2016
Beach
81
50
74
90.0
0.50
17
7/17/2016
Beach
115
76
77
126.0
0.50
18
7/18/2016
Park
131
92
81
122.0
0.50
19
7/19/2016
Park
122
85
78
113.0
0.50
20
7/20/2016
Park
71
42
70
NaN
0.50
21
7/21/2016
Park
83
50
77
90.0
0.50
22
7/22/2016
Park
112
75
80
108.0
0.50
23
7/23/2016
Park
120
82
81
117.0
0.50
24
7/24/2016
Park
121
82
82
117.0
0.50
25
7/25/2016
Park
156
113
84
135.0
0.50
26
7/26/2016
Park
176
129
83
158.0
0.35
27
7/27/2016
Park
104
68
80
99.0
0.35
28
7/28/2016
Park
96
63
82
90.0
0.35
29
7/29/2016
Park
100
66
81
95.0
0.35
30
7/30/2016
Beach
88
57
82
81.0
0.35
31
7/31/2016
Beach
76
47
82
68.0
0.35
<script>
const buttonEl =
document.querySelector('#df-377049de-a05e-4731-8fe4-a3adc97f89dd button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-377049de-a05e-4731-8fe4-a3adc97f89dd');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
<script>
const buttonEl =
document.querySelector('#df-fdaed60a-bf98-46fe-a701-24e216acb756 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-fdaed60a-bf98-46fe-a701-24e216acb756');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
1 2 3 4
juice2 = juice.sort_values(by=['Price', 'Temperature'], ascending = [False, True]).reset_index(drop=True) juice2 # 가격과 온도에 따라서 정렬을 해주다보니 인덱스 번호가 뒤죽박죽이 되어버렸다. 인덱스번호를 리셋 해주고 새로운 데이터셋으로 만들어준 것이다. # 그리고 juice2라는 새로운 객체에 저장해준 것.
Date
Location
Lemon
Orange
Temperature
Leaflets
Price
sold
Revenue
0
7/20/2016
Park
71
42
70
NaN
0.50
113
56.50
1
7/16/2016
Beach
81
50
74
90.0
0.50
131
65.50
2
7/15/2016
Beach
98
62
75
108.0
0.50
160
80.00
3
7/17/2016
Beach
115
76
77
126.0
0.50
191
95.50
4
7/21/2016
Park
83
50
77
90.0
0.50
133
66.50
5
7/19/2016
Park
122
85
78
113.0
0.50
207
103.50
6
7/22/2016
Park
112
75
80
108.0
0.50
187
93.50
7
7/18/2016
Park
131
92
81
122.0
0.50
223
111.50
8
7/23/2016
Park
120
82
81
117.0
0.50
202
101.00
9
7/24/2016
Park
121
82
82
117.0
0.50
203
101.50
10
7/25/2016
Park
156
113
84
135.0
0.50
269
134.50
11
7/27/2016
Park
104
68
80
99.0
0.35
172
60.20
12
7/29/2016
Park
100
66
81
95.0
0.35
166
58.10
13
7/28/2016
Park
96
63
82
90.0
0.35
159
55.65
14
7/30/2016
Beach
88
57
82
81.0
0.35
145
50.75
15
7/31/2016
Beach
76
47
82
68.0
0.35
123
43.05
16
7/26/2016
Park
176
129
83
158.0
0.35
305
106.75
17
7/1/2016
Park
97
67
70
90.0
0.25
164
41.00
18
7/3/2016
Park
110
77
71
104.0
0.25
187
46.75
19
7/2/2016
Park
98
67
72
90.0
0.25
165
41.25
20
7/4/2016
Beach
134
99
76
98.0
0.25
233
58.25
21
7/13/2016
Beach
109
75
77
99.0
0.25
184
46.00
22
7/5/2016
Beach
159
118
78
135.0
0.25
277
69.25
23
7/14/2016
Beach
122
85
78
113.0
0.25
207
51.75
24
7/9/2016
Beach
134
95
80
126.0
0.25
229
57.25
25
7/7/2016
Beach
143
101
81
135.0
0.25
244
61.00
26
7/6/2016
Beach
103
69
82
90.0
0.25
172
43.00
27
7/6/2016
Beach
103
69
82
90.0
0.25
172
43.00
28
NaN
Beach
123
86
82
113.0
0.25
209
52.25
29
7/10/2016
Beach
140
98
82
131.0
0.25
238
59.50
30
7/11/2016
Beach
162
120
83
135.0
0.25
282
70.50
31
7/12/2016
Beach
130
95
84
99.0
0.25
225
56.25
<script>
const buttonEl =
document.querySelector('#df-374e41cd-177f-4906-a3ce-e839f106f7a7 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-374e41cd-177f-4906-a3ce-e839f106f7a7');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
Groupby()
피벗 테이블을 만드는 것과 똑같음
요약하려고
1
juice.groupby(by = 'Location').count()
Date
Lemon
Orange
Temperature
Leaflets
Price
sold
Revenue
Location
Beach
16
17
17
17
17
17
17
17
Park
15
15
15
15
14
15
15
15
<script>
const buttonEl =
document.querySelector('#df-9d46cfb2-4ae7-44c5-a9de-d88d60478500 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-9d46cfb2-4ae7-44c5-a9de-d88d60478500');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
1 2
import numpy as np juice.groupby(['Location'])[['Revenue', 'Lemon']].agg([max, min, sum, np.mean])
Revenue
Lemon
max
min
sum
mean
max
min
sum
mean
Location
Beach
95.5
43.0
1002.8
58.988235
162
76
2020
118.823529
Park
134.5
41.0
1178.2
78.546667
176
71
1697
113.133333
<script>
const buttonEl =
document.querySelector('#df-d175d024-02cf-46a7-a0ef-d792b2802f03 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-d175d024-02cf-46a7-a0ef-d792b2802f03');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>