import pandas as pd
data = pd.Series([0.25, 0.5, 0.75, 1.0])
data
0 0.25 1 0.50 2 0.75 3 1.00 dtype: float64
import numpy as np
np.array([0.25, 0.5, 0.75, 1.0])
array([0.25, 0.5 , 0.75, 1. ])
data.values
array([0.25, 0.5 , 0.75, 1. ])
data.index
RangeIndex(start=0, stop=4, step=1)
data[2]
0.75
data[1:3]
1 0.50 2 0.75 dtype: float64
data = pd. Series ([0.25 , 0.5 , 0.75 , 1.0] ,
index =["a", "b", "c", "d"])
data
a 0.25 b 0.50 c 0.75 d 1.00 dtype: float64
data.index
Index(['a', 'b', 'c', 'd'], dtype='object')
data[1:3]
b 0.50 c 0.75 dtype: float64
data["a"]
0.25
users_dict = {"Alice ": "Lidell ",
"Bob": "Ross",
"Charlie":"Chaplin"}
data = pd.Series(users_dict)
data
Alice Lidell Bob Ross Charlie Chaplin dtype: object
data.index
Index(['Alice ', 'Bob', 'Charlie'], dtype='object')
data.values
array(['Lidell ', 'Ross', 'Chaplin'], dtype=object)
data.describe()
count 3 unique 3 top Lidell freq 1 dtype: object
area_dict = {"Vienna": 415, "Lower Austria": 19178,
"Styria": 16401, "Upper Austria": 11982,
"Tyrol": 12648, "Carinthia": 9536,
"Salzburg": 7154,"Vorarlberg": 2601,
"Burgenland": 3965}
pop_dict = {"Vienna": 1794770, "Lower Austria": 1636287,
"Styria": 1221014, "Upper Austria": 1436791,
"Tyrol": 728537, "Carinthia": 557371,
"Salzburg": 538258,"Vorarlberg": 378490,
"Burgenland": 288229}
area = pd.Series(area_dict)
pop = pd.Series(pop_dict)
states = pd.DataFrame({"area": area, "population": pop})
states
area | population | |
---|---|---|
Vienna | 415 | 1794770 |
Lower Austria | 19178 | 1636287 |
Styria | 16401 | 1221014 |
Upper Austria | 11982 | 1436791 |
Tyrol | 12648 | 728537 |
Carinthia | 9536 | 557371 |
Salzburg | 7154 | 538258 |
Vorarlberg | 2601 | 378490 |
Burgenland | 3965 | 288229 |
states.loc[["Vienna", "Lower Austria"], "population"]
Vienna 1794770 Lower Austria 1636287 Name: population, dtype: int64
states.describe()
area | population | |
---|---|---|
count | 9.000000 | 9.000000e+00 |
mean | 9320.000000 | 9.533052e+05 |
std | 6357.483543 | 5.736115e+05 |
min | 415.000000 | 2.882290e+05 |
25% | 3965.000000 | 5.382580e+05 |
50% | 9536.000000 | 7.285370e+05 |
75% | 12648.000000 | 1.436791e+06 |
max | 19178.000000 | 1.794770e+06 |
states["population"] < 1e6
Vienna False Lower Austria False Styria False Upper Austria False Tyrol True Carinthia True Salzburg True Vorarlberg True Burgenland True Name: population, dtype: bool
states["density"] = states["population"]/states["area"]
print(states.describe())
states
area population density count 9.000000 9.000000e+00 9.000000 mean 9320.000000 9.533052e+05 557.103033 std 6357.483543 5.736115e+05 1413.162150 min 415.000000 2.882290e+05 57.600965 25% 3965.000000 5.382580e+05 72.693317 50% 9536.000000 7.285370e+05 75.238748 75% 12648.000000 1.436791e+06 119.912452 max 19178.000000 1.794770e+06 4324.746988
area | population | density | |
---|---|---|---|
Vienna | 415 | 1794770 | 4324.746988 |
Lower Austria | 19178 | 1636287 | 85.321045 |
Styria | 16401 | 1221014 | 74.447534 |
Upper Austria | 11982 | 1436791 | 119.912452 |
Tyrol | 12648 | 728537 | 57.600965 |
Carinthia | 9536 | 557371 | 58.449140 |
Salzburg | 7154 | 538258 | 75.238748 |
Vorarlberg | 2601 | 378490 | 145.517109 |
Burgenland | 3965 | 288229 | 72.693317 |
states.sort_values(by=["density"])
area | population | density | |
---|---|---|---|
Tyrol | 12648 | 728537 | 57.600965 |
Carinthia | 9536 | 557371 | 58.449140 |
Burgenland | 3965 | 288229 | 72.693317 |
Styria | 16401 | 1221014 | 74.447534 |
Salzburg | 7154 | 538258 | 75.238748 |
Lower Austria | 19178 | 1636287 | 85.321045 |
Upper Austria | 11982 | 1436791 | 119.912452 |
Vorarlberg | 2601 | 378490 | 145.517109 |
Vienna | 415 | 1794770 | 4324.746988 |
import copy
list1 = [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
list2 = list1
list3 = copy.copy(list1)
list4 = copy.deepcopy(list1)
print('1\t', id(list1), '\t', list1)
print('1\t', id(list2), '\t', list2)
print('1\t', id(list3), '\t', list3)
print('1\t', id(list4), '\t', list4)
list2[2][2] = 9
1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']] 1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']] 1 140671569958784 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']] 1 140671574046592 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
print('1\t', id(list1), '\t', list1)
print('1\t', id(list2), '\t', list2)
print('1\t', id(list3), '\t', list3)
print('1\t', id(list4), '\t', list4)
1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9]] 1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9]] 1 140671569958784 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9]] 1 140671574046592 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
print(id(list1[2]))
print(id(list3[2]))
print(id(list4[2]))
140671566459776 140671566459776 140671577480256
list1.append([0, 8, 15])
print('1\t', id(list1), '\t', list1)
print('1\t', id(list2), '\t', list2)
print('1\t', id(list3), '\t', list3)
print('1\t', id(list4), '\t', list4)
1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9], [0, 8, 15], [0, 8, 15]] 1 140671682972352 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9], [0, 8, 15], [0, 8, 15]] 1 140671569958784 [[1, 2, 3], [4, 5, 6], ['a', 'b', 9]] 1 140671574046592 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
list1[1][1]='a'
print('1\t', id(list1), '\t', list1)
print('1\t', id(list2), '\t', list2)
print('1\t', id(list3), '\t', list3)
print('1\t', id(list4), '\t', list4)
1 140671682972352 [[1, 2, 3], [4, 'a', 6], ['a', 'b', 9], [0, 8, 15], [0, 8, 15]] 1 140671682972352 [[1, 2, 3], [4, 'a', 6], ['a', 'b', 9], [0, 8, 15], [0, 8, 15]] 1 140671569958784 [[1, 2, 3], [4, 'a', 6], ['a', 'b', 9]] 1 140671574046592 [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
a = 1
b = 1
c = a
print('a\t', id(a))
print('b\t', id(b))
print('c\t', id(c))
a 140674738766064 b 140674738766064 c 140674738766064
d = [1, 2, 3]
d
[1, 2, 3]
d *= 2
d
[1, 2, 3, 1, 2, 3]
d_np = np.array([1, 2, 3])
print(d_np)
print(id(d_np))
[1 2 3] 140671577315376
d_np *= 2
print(d_np)
print(id(d_np))
[1 2 3] 140671577315376