Python
-
[matplotlib] Merge two graphs into one
twinx() import pandas as pd import matplotlib.pyplot as plt import numpy as np f, ax = plt.subplots(1,1) x_values = [0,1,2,3,4,5] y_values = [0,1,4,9,16,25] ax.plot(x_values, y_values, color='red', linestyle='dashed', linewidth=1, marker="o") x2_values = [0,1,2,3,4,5] y2_values = [1000,2000,3000,4000,5000,6000] ax2=ax.twinx() ax2.plot(x2_values, y2_values, color='darkblue', linestyle='dashed', l..
-
[pandas] merge/concat data-frame
Sometimes, two data set should be merged to provide more accurate information. Merging data frame is divided into two parts: vertical and horizontal merging. 1) Vertical merging import pandas as pd df1 = pd.DataFrame({'A' : [1, 2, 3], 'B' : [11, 12, 13], 'C' : [21, 22, 23]}) df2 = pd.DataFrame({'A' : [4, 5, 6], 'B' : [14, 15, 16], 'C' : [24, 25, 26]}) print(df.concat([df1, df2])) print(df.concat..
-
[pandas] Data transformation - using current data to classify
import pandas as pd df = pd.DataFrame({'a':[1,2,3,4,5]}) # create a 'b' column if a= 2)] df['b'][a.index] = 's4' a = df[df['a'] > 4] df['b'][a.index] = 'b4' print(df) # Second method. using 'Apply with function' def apply_function(a): if a < 2: return 's2' elif a < 4: return 's4' else: return 'b4' df['b'] = df['a'].apply(apply_function) # Map - when there is a certain categories without conditio..
-
[pandas] add new column / delete column
1. Column import pandas as pd df = pd.DataFrame({'a': [1,1,3,4,5], 'b': [2,3,2,3,4], 'c': [3,4,7,6,4]}) # 1. to simply add new column df['d'] = [1,3,6,4,8] # 2. add one number in that column (will be filled out with that number) df['e'] = 1 # +) calculated result can be also created as a new column. # check datatype first print(df.dtypes) df['f'] = df['a'] + df['b'] - df['c'] # delete a column d..
-
[pandas] Data type conversion (astype etc...)
import pandas as pd df = pd.DataFrame({'date' : ['5/11/21', '5/12/21', '5/13/21', '5/14/21', '5/15/21'], 'sales' : ['10', '15', '20', '25', '30'], 'visitors' : ['10', '-', '17', '23', '25'], 'temp.' : ['24.1', '24.3', '24.8', '25', '25.4']}) # we need to check data type for each column (to change/edit data) print(df.dtypes) # try to change the value without data conversion df['edited sales'] = d..
Recent
-
[The Economist] Apple’s Vision Pro is a technical marvel. Will anyone buy it?Economist 2023.06.06 15:21
vocab - sleek: smooth and glossy as if polished sleek dark hair // having a smooth well-groomed look - commercial flop: complete failure - in lieu of: in the place of : instead of - cluncky: clumsy in style - retrench: cut down - hoover up: "to remove something from a floor or other surface using a vacuum cleaner (= a machine that sucks up dust and dirt):" (Cambridge Dictionary) Summary - Unlike..
-
[The Economist] Will drone cops make American policing safer or just more intrusive?Economist 2023.06.03 17:06
Drone cop (+: vantage point) It helps split-second decision (+) It offers profound evidence, while camera that officer currently wears do not show what the officer is doing (-) It is an equipment that police disrespect "citizen's right to policy" vocab - reconnaissance: preliminary survey to gain information - galvanize: to stimulate or excite as if by an electric shock - apprehend: arrest, seiz..
-
[Economist] Can the West win over the rest?Economist 2023.04.15 21:42
- People often regard that the world is divided into two: Western bloc and alliance of China and Russia. However, it is wrong. More than 100 countires ("non-aligned countries") prefer neither each side. Their clout is even considerable. - Their economic power is growing; 45% of world's population, 18% of global GDP. - They often combine their power such as OPEC. Vocab (definition from Merriam-We..
-
[Economist] Can high-rise buildings solve London’s housing problems?Economist 2023.04.13 14:46
Summary - Britain needs more houses due to population growth, especially in cities, but the supply of new housing is "not keeping up". (strict planning rules: green-belt around cities) - One way to accommodate more homes in a restricted area to construct is building residential skyscrapers (as New York and Hong Kong do) However, there are set of hurdles. 1) Tall buildings are expensive to constr..
-
[Economist] Stocks have shrugged off the banking turmoil. Haven’t they?Economist 2023.04.11 08:51
Summary Bank failure always led the decrease of stock market. However, this time is different. 1. Investors are "betting on rate-cuts," (they expect the interest rates to decrease) and therefore are investing insectors that are more sensitive interest rates (such as tech companies). (Contrastingly, as real estate or financial companies is dependent on borrowing, so the increase in interest rates..