-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataframe基本用法.py
147 lines (68 loc) · 1.25 KB
/
dataframe基本用法.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# coding: utf-8
# In[7]:
import numpy as np
import pandas as pd
# In[3]:
a = {"a":[1,2,3],"b":[4,5,6],"c":[7,8,9]}
# In[8]:
b = pd.DataFrame(a)
取列
# In[12]:
#返回<class 'pandas.core.series.Series'>
print(b.a)
print(type(b.a))
# In[13]:
#返回<class 'pandas.core.series.Series'>
print(b["b"])
print(type(b["b"]))
# In[14]:
#返回<class 'pandas.core.frame.DataFrame'>
print(b[["b"]])
print(type(b[["b"]]))
# In[19]:
print(b[["a":"b"]])
取行
# In[17]:
#前闭后开
b[0:2]
# iloc可以同时取行和列
# In[27]:
#<class 'pandas.core.series.Series'>
print(b.iloc[0])
print(type(b.iloc[0]))
# In[28]:
b.iloc[0:2]
# In[29]:
b.iloc[:,1]
# In[31]:
#<class 'numpy.ndarray'>
print(b.values)
print(type(b.values))
# ix取某一行(不建议使用)
# In[32]:
b.ix[0]
loc和iloc的区别
# In[33]:
#左右都闭
b.loc[0:2]
# In[34]:
#左开右闭
b.iloc[0:2]
# In[42]:
#切片,列部分不用加[]
b.iloc[:,0:3]
# In[50]:
#error行标签已经定义了,无法用loc数字切片
#b.loc[:,[0:3]]
# In[51]:
#错误方法
#b.iloc[:,"a":"c"]
#正确写法
b.iloc[:,0:3]
# In[53]:
#loc切片不用加[]
b.loc[:,"a":"c"]
# In[54]:
#loc选多个标签需要加[]
b.loc[:,["a","b"]]