forked from yzziqiu/python-class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek-8-pandas.py
More file actions
72 lines (61 loc) · 1.33 KB
/
week-8-pandas.py
File metadata and controls
72 lines (61 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# week 9 pandas.py
import pandas as import pd
import numpy as np
def sep():
print("________________________")
s = pd.Series(data=5, index=["a", "b", "c", "d", "e"])
print(s)
sep()
s = pd.Series(data=np.random.randn(5), index=["a", "b", "c", "d", "e"])
print(s)
sep()
s = pd.Series(data=np.random.randn(5))
print(s)
sep()
data = {'a':3.2, 'b':1, 'c':1.0, 'd':10, 'e':5}
s = pd.Series(data)
print(s)
# series like dictionary, single-level
sep()
print('a' in s) #true
print('z' in s) #false
print(s['a'])
sep()
print(s.sum())
sep()
# add 1 to each value
print(s+1)
print(s*2)
s2 = pd.Series(data=np.random.randn(5))
s3 = pd.Series(data=np.random.randn(10))
print(s2+s3)
sep()
data = {'a':3.2, 'b':1, 'c':"1.0", 'd':10, 'e':5}
s = pd.Series(data)
print(s)
#object
sep()
s = pd.Series(data=data, index['a','e'])
print(s)
data={
'studentid':['a','b','c','d','e'],
'gender':['m','f','m','f','m'],
'score':[80,70,60,95,87]
}
dt = pd.DataFrame(data)
print(df)
df.set_index(['studentid'], inplace =True)
print(df)
# first 2 rows
print(df.head(2))
print(df.tail(2))
sep()
print(df['gender'])
print(df.gender) # name , type
print(df.loc['e'])
#select row
print(df.iloc[4]),
# slice row df[n:m]
print(df.df[:2])
df = pd.read_csv('pokemon.csv', index_col='#')
print(df.head())