import seaborn as sns
import pandas as pd
dataset = sns.load_dataset("tips")
print(dataset)
print(dataset.shape)
print(dataset.columns)
print("list total_bill > 30:")
print(dataset[ dataset['total_bill'] > 30 ] )
然而,預設 Pandas 會記錄原先的 raw index ,這也有不錯的功用,但有時希望照新的架構顯示,需要再多用 reset_index():
print("list total_bill > 30 and tip < 4:")
print(dataset[ (dataset['total_bill'] > 30) & (dataset['tip'] < 4) ] )
print("rebuild index:")
dataset = dataset[ (dataset['total_bill'] > 30) & (dataset['tip'] < 4) ]
dataset = dataset.reset_index()
print(dataset)
連續動作:
$ python pandas_study.py
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
5 25.29 4.71 Male No Sun Dinner 4
6 8.77 2.00 Male No Sun Dinner 2
7 26.88 3.12 Male No Sun Dinner 4
8 15.04 1.96 Male No Sun Dinner 2
9 14.78 3.23 Male No Sun Dinner 2
10 10.27 1.71 Male No Sun Dinner 2
11 35.26 5.00 Female No Sun Dinner 4
12 15.42 1.57 Male No Sun Dinner 2
13 18.43 3.00 Male No Sun Dinner 4
14 14.83 3.02 Female No Sun Dinner 2
15 21.58 3.92 Male No Sun Dinner 2
16 10.33 1.67 Female No Sun Dinner 3
17 16.29 3.71 Male No Sun Dinner 3
18 16.97 3.50 Female No Sun Dinner 3
19 20.65 3.35 Male No Sat Dinner 3
20 17.92 4.08 Male No Sat Dinner 2
21 20.29 2.75 Female No Sat Dinner 2
22 15.77 2.23 Female No Sat Dinner 2
23 39.42 7.58 Male No Sat Dinner 4
24 19.82 3.18 Male No Sat Dinner 2
25 17.81 2.34 Male No Sat Dinner 4
26 13.37 2.00 Male No Sat Dinner 2
27 12.69 2.00 Male No Sat Dinner 2
28 21.70 4.30 Male No Sat Dinner 2
29 19.65 3.00 Female No Sat Dinner 2
.. ... ... ... ... ... ... ...
214 28.17 6.50 Female Yes Sat Dinner 3
215 12.90 1.10 Female Yes Sat Dinner 2
216 28.15 3.00 Male Yes Sat Dinner 5
217 11.59 1.50 Male Yes Sat Dinner 2
218 7.74 1.44 Male Yes Sat Dinner 2
219 30.14 3.09 Female Yes Sat Dinner 4
220 12.16 2.20 Male Yes Fri Lunch 2
221 13.42 3.48 Female Yes Fri Lunch 2
222 8.58 1.92 Male Yes Fri Lunch 1
223 15.98 3.00 Female No Fri Lunch 3
224 13.42 1.58 Male Yes Fri Lunch 2
225 16.27 2.50 Female Yes Fri Lunch 2
226 10.09 2.00 Female Yes Fri Lunch 2
227 20.45 3.00 Male No Sat Dinner 4
228 13.28 2.72 Male No Sat Dinner 2
229 22.12 2.88 Female Yes Sat Dinner 2
230 24.01 2.00 Male Yes Sat Dinner 4
231 15.69 3.00 Male Yes Sat Dinner 3
232 11.61 3.39 Male No Sat Dinner 2
233 10.77 1.47 Male No Sat Dinner 2
234 15.53 3.00 Male Yes Sat Dinner 2
235 10.07 1.25 Male No Sat Dinner 2
236 12.60 1.00 Male Yes Sat Dinner 2
237 32.83 1.17 Male Yes Sat Dinner 2
238 35.83 4.67 Female No Sat Dinner 3
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2
[244 rows x 7 columns]
(244, 7)
Index(['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size'], dtype='object')
list total_bill > 30:
total_bill tip sex smoker day time size
11 35.26 5.00 Female No Sun Dinner 4
23 39.42 7.58 Male No Sat Dinner 4
39 31.27 5.00 Male No Sat Dinner 3
44 30.40 5.60 Male No Sun Dinner 4
47 32.40 6.00 Male No Sun Dinner 4
52 34.81 5.20 Female No Sun Dinner 4
56 38.01 3.00 Male Yes Sat Dinner 4
59 48.27 6.73 Male No Sat Dinner 4
83 32.68 5.00 Male Yes Thur Lunch 2
85 34.83 5.17 Female No Thur Lunch 4
95 40.17 4.73 Male Yes Fri Dinner 4
102 44.30 2.50 Female Yes Sat Dinner 3
112 38.07 4.00 Male No Sun Dinner 3
141 34.30 6.70 Male No Thur Lunch 6
142 41.19 5.00 Male No Thur Lunch 5
156 48.17 5.00 Male No Sun Dinner 6
167 31.71 4.50 Male No Sun Dinner 4
170 50.81 10.00 Male Yes Sat Dinner 3
173 31.85 3.18 Male Yes Sun Dinner 2
175 32.90 3.11 Male Yes Sun Dinner 2
179 34.63 3.55 Male Yes Sun Dinner 2
180 34.65 3.68 Male Yes Sun Dinner 4
182 45.35 3.50 Male Yes Sun Dinner 3
184 40.55 3.00 Male Yes Sun Dinner 2
187 30.46 2.00 Male Yes Sun Dinner 5
197 43.11 5.00 Female Yes Thur Lunch 4
207 38.73 3.00 Male Yes Sat Dinner 4
210 30.06 2.00 Male Yes Sat Dinner 3
212 48.33 9.00 Male No Sat Dinner 4
219 30.14 3.09 Female Yes Sat Dinner 4
237 32.83 1.17 Male Yes Sat Dinner 2
238 35.83 4.67 Female No Sat Dinner 3
list total_bill > 30 and tip < 4:
print index:
index: 56
index: 102
index: 173
index: 175
index: 179
index: 180
index: 182
index: 184
index: 187
index: 207
index: 210
index: 219
index: 237
rebuild index:
index total_bill tip sex smoker day time size
0 56 38.01 3.00 Male Yes Sat Dinner 4
1 102 44.30 2.50 Female Yes Sat Dinner 3
2 173 31.85 3.18 Male Yes Sun Dinner 2
3 175 32.90 3.11 Male Yes Sun Dinner 2
4 179 34.63 3.55 Male Yes Sun Dinner 2
5 180 34.65 3.68 Male Yes Sun Dinner 4
6 182 45.35 3.50 Male Yes Sun Dinner 3
7 184 40.55 3.00 Male Yes Sun Dinner 2
8 187 30.46 2.00 Male Yes Sun Dinner 5
9 207 38.73 3.00 Male Yes Sat Dinner 4
10 210 30.06 2.00 Male Yes Sat Dinner 3
11 219 30.14 3.09 Female Yes Sat Dinner 4
12 237 32.83 1.17 Male Yes Sat Dinner 2
print index:
index: 0
index: 1
index: 2
index: 3
index: 4
index: 5
index: 6
index: 7
index: 8
index: 9
index: 10
index: 11
index: 12
沒有留言:
張貼留言