[3.5, 4.6, 5.7]
A particular way of organizing data in a computer
A collection allows us to put many values in a single “variable”
[ ],依序將list結構的內容取出,設定為x,並印出來
變數名稱[index]index從0開始| 位置 | 1 | 2 | 3 | 4 | … |
|---|---|---|---|---|---|
| index | 0 | 1 | 2 | 3 | … |
start:up to but not including
range(數列起點,數列終點[不包含],間隔)宣告We can create a new list by adding + two existing lists together
We can create an empty list and then add elements using the append method
sum(list物件名稱) 物件加總
max(list物件名稱) 物件最大值
可參考文件
list…( ),.append(), .sort(), etcWe can also put a tuple on the left-hand side of an assignment statement
a,包含數字1到10b,包含數字1到20中的所有偶數a sequence 的第4個值{ }宣告key : value的組合,並以,分隔變數名稱[key][key1, key2, …]
[value1, value2, …]
[(key1, value1), (key2, value2),…]
mapping物件.update()可新增或修改內容(key:value pairs)至mapping中[ ]取值後寫回
.get(key, 0) 取值
dist1,內容為id中的第3個值name中的第2個值的第1個字元(字母).values()即可取value (值)先得到key,再取得value
id [1, 2, 3, 4]
name ['Ryan', 'Tom', 'Emma', 'Amy']
School NYCU
或是直接使用.items()
Most Common Name? Edit the ???? parts
Use dist and for to get the most common name and its count
| Col1 | Col2 | Col3 |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
numpy library (Numerical Python)list type, but arrays provide much more efficient storage and data operations (fixed-type).array(list) to create numpy array from list或是用.arrange(起點,終點[不包含],間隔)來生成numpy array,如同range()的用法
np.full(dim, value): repeated valuesWill be covered in Ch 5
Will be covered in Ch 5
sum()、mean()、std()、cumsum()、max()、min()、count()
sum()、mean()、std()、cumsum()、max()、min()、count()
axis= 0 BY COLUMN, 1 BY ROW
Series, DataFrame, and Index
pandas librarypd.Series(list)0 0.25
1 0.50
2 0.75
3 1.00
dtype: float64
index
a 0.25
b 0.50
c 0.75
d 1.00
dtype: float64
pd.Series(dictionary)
population_dict = {'California': 38332521,
'Texas': 26448193,
'New York': 19651127,
'Florida': 19552860,
'Illinois': 12882135}
population = pd.Series(population_dict)
area_dict = {'California': 423967,
'Texas': 695662,
'New York': 141297,
'Florida': 170312,
'Illinois': 149995}
area = pd.Series(area_dict)
states = pd.DataFrame({'population': population,
'area': area})
states| population | area | |
|---|---|---|
| California | 38332521 | 423967 |
| Texas | 26448193 | 695662 |
| New York | 19651127 | 141297 |
| Florida | 19552860 | 170312 |
| Illinois | 12882135 | 149995 |
資料框物件名稱.head(資料筆數):取前幾筆
資料框物件名稱.head(資料筆數):取後幾筆
資料框物件名稱[欄位名稱]:取出sequence
資料框物件名稱[[欄位名稱]]:取出資料框
資料框物件名稱[row slice]
預設index為0~n的序列,可用index參數修改
也可用已有的data frame設定,透過.set_index(欄位名稱)
pd.concat([pd物件1, pd物件2]),預設為row方向的合併
使用type()函數可查看資料結構
[,](,)range(s,e,i){key:value,key:value}