In [1]:
import pandas as pd
In [3]:
data = pd.Series([0.25, 0.5, 0.75, 1.0])
data
Out[3]:
0    0.25
1    0.50
2    0.75
3    1.00
dtype: float64
In [6]:
import numpy as np
np.array([0.25, 0.5, 0.75, 1.0])
Out[6]:
array([0.25, 0.5 , 0.75, 1.  ])
In [7]:
data.values
Out[7]:
array([0.25, 0.5 , 0.75, 1.  ])
In [8]:
data.index
Out[8]:
RangeIndex(start=0, stop=4, step=1)
In [9]:
data[2]
Out[9]:
0.75
In [10]:
data[1:3]
Out[10]:
1    0.50
2    0.75
dtype: float64
In [11]:
data = pd. Series ([0.25 , 0.5 , 0.75 , 1.0] ,
                  index =["a",  "b",   "c", "d"])
In [12]:
data
Out[12]:
a    0.25
b    0.50
c    0.75
d    1.00
dtype: float64
In [13]:
data.index
Out[13]:
Index(['a', 'b', 'c', 'd'], dtype='object')
In [14]:
data[1:3]
Out[14]:
b    0.50
c    0.75
dtype: float64
In [15]:
data["a"]
Out[15]:
0.25
In [20]:
users_dict = {"Alice ": "Lidell ",
              "Bob":    "Ross",
              "Charlie":"Chaplin"}
data = pd.Series(users_dict)
data
Out[20]:
Alice      Lidell 
Bob           Ross
Charlie    Chaplin
dtype: object
In [21]:
data.index
Out[21]:
Index(['Alice ', 'Bob', 'Charlie'], dtype='object')
In [22]:
data.values
Out[22]:
array(['Lidell ', 'Ross', 'Chaplin'], dtype=object)
In [23]:
data.describe()
Out[23]:
count           3
unique          3
top       Lidell 
freq            1
dtype: object
In [24]:
area_dict = {"Vienna": 415,   "Lower Austria": 19178,
                 "Styria": 16401, "Upper Austria": 11982,
                 "Tyrol": 12648,  "Carinthia": 9536,
                 "Salzburg": 7154,"Vorarlberg": 2601,
                 "Burgenland": 3965}
In [25]:
pop_dict = {"Vienna": 1794770, "Lower Austria": 1636287,
                "Styria": 1221014, "Upper Austria": 1436791,
                "Tyrol": 728537,   "Carinthia": 557371,
                "Salzburg": 538258,"Vorarlberg": 378490,
                "Burgenland": 288229}
In [26]:
area = pd.Series(area_dict)
pop = pd.Series(pop_dict)
states = pd.DataFrame({"area": area, "population": pop})
In [27]:
states
Out[27]:
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
In [29]:
states.loc[["Vienna", "Lower Austria"], "population"]
Out[29]:
Vienna           1794770
Lower Austria    1636287
Name: population, dtype: int64
In [32]:
states.describe()
Out[32]:
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
In [33]:
states["population"] < 1e6
Out[33]:
Vienna           False
Lower Austria    False
Styria           False
Upper Austria    False
Tyrol             True
Carinthia         True
Salzburg          True
Vorarlberg        True
Burgenland        True
Name: population, dtype: bool
In [34]:
states["density"] = states["population"]/states["area"]
In [36]:
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
Out[36]:
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
In [37]:
states.sort_values(by=["density"])
Out[37]:
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
In [39]:
import copy
In [41]:
list1 = [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
list2 = list1
list3 = copy.copy(list1)
list4 = copy.deepcopy(list1)
In [46]:
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']]
In [47]:
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']]
In [50]:
print(id(list1[2]))
print(id(list3[2]))
print(id(list4[2]))
140671566459776
140671566459776
140671577480256
In [52]:
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']]
In [54]:
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']]
In [ ]:
 
In [55]:
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
In [69]:
d = [1, 2, 3]
In [70]:
d
Out[70]:
[1, 2, 3]
In [71]:
d *= 2
In [72]:
d
Out[72]:
[1, 2, 3, 1, 2, 3]
In [76]:
d_np = np.array([1, 2, 3])
print(d_np)
print(id(d_np))
[1 2 3]
140671577315376
In [74]:
d_np *= 2
In [77]:
print(d_np)
print(id(d_np))
[1 2 3]
140671577315376