Trong Python [theo đề xuất của @sahasrara62 trong các bình luận]
ages = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
changes = []
for x1, x2 in zip[ages[:-1], ages[1:]]:
try:
pct = [x2 - x1] * 100 / x1
except ZeroDivisionError:
pct = None
changes.append[pct]
# [50.2463054187192,
# -33.44262295081967,
# 124.13793103448275,
# 11.208791208791212,
# -41.699604743083,
# -54.576271186440685,
# 951.4925373134328]
Sử dụng Numpy
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
Sử dụng gấu trúc
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
Tính tỷ lệ phần trăm trong Python #
Để tính tỷ lệ phần trăm trong Python:
- Sử dụng toán tử phân chia
2 để chia một số cho một số khác.import numpy as np ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]] diff = ages[1:] - ages[:-1] changes = diff / ages[1:] * 100 # [ 50.24630542 -33.44262295 124.13793103 11.20879121 # -41.69960474 -54.57627119 951.49253731]
- Nhân số chỉ số với
3 để có được tỷ lệ phần trăm.import numpy as np ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]] diff = ages[1:] - ages[:-1] changes = diff / ages[1:] * 100 # [ 50.24630542 -33.44262295 124.13793103 11.20879121 # -41.69960474 -54.57627119 951.49253731]
- Kết quả cho thấy bao nhiêu phần trăm số đầu tiên của số thứ hai.
Copied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
Hàm đầu tiên mất 2 số và trả về phần trăm số đầu tiên của số thứ hai.
Ví dụ,
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
4 cho thấy import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
5 là import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
6 của import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
7.Copied!
print[[25 / 50] * 100] # 👉️ 50.0
Khi tính toán tỷ lệ phần trăm, bạn có thể cần làm tròn đến một số chữ số cụ thể sau thập phân.
Hàm vòng lấy 2 tham số sau:
8 | con số đến vòng đến độ chính xác 9 sau thập phân |
9 | Số chữ số sau số thập phân Số lượng nên có sau khi hoạt động [tùy chọn] |
Copied!
print[round[[33 / 65] * 100, 2]] # 👉️ 50.77
Hàm
1 trả về số được làm tròn đến độ chính xác của import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
9 sau điểm thập phân.import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
Nếu
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
9 bị bỏ qua, hàm trả về số nguyên gần nhất.Lưu ý rằng nếu bạn cố gắng chia cho
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
4, bạn sẽ nhận được import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
5.Nếu bạn cần xử lý việc này theo bất kỳ cách nào, hãy sử dụng khối
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
6 để xử lý lỗi.Copied!
def is_what_percent_of[num_a, num_b]: try: return [num_a / num_b] * 100 except ZeroDivisionError: return 0 print[is_what_percent_of[25, 0]] # 👉️ 0
Hàm thứ hai cho thấy làm thế nào để có được phần trăm tăng / giảm giữa hai số.
Copied!
def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0
Ví dụ đầu tiên cho thấy tỷ lệ phần trăm tăng từ
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
7 lên import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
8 là import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
9.Và ví dụ thứ hai cho thấy tỷ lệ phần trăm tăng từ
0 lênCopied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
3 là 2.Copied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
Nếu bạn luôn cần có một số dương, hãy sử dụng hàm
3.Copied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
Copied!
def get_percentage_increase[num_a, num_b]: return abs[[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ 60.0
Hàm ABS trả về giá trị tuyệt đối của một số. Nói cách khác, nếu số là dương, số được trả về và nếu số là âm, thì phủ định của số được trả về.
Bằng cách này, chúng tôi luôn được đảm bảo để có được một số dương khi tính toán sự khác biệt về tỷ lệ phần trăm giữa hai số.
Bạn cũng có thể cần xử lý bộ phận theo trường hợp bằng không.
Copied!
def get_percentage_increase[num_a, num_b]: try: return abs[[num_a - num_b] / num_b] * 100 except ZeroDivisionError: return float['inf'] print[get_percentage_increase[60, 0]] # 👉️ inf print[get_percentage_increase[60, 60]] # 👉️ 0.0 print[get_percentage_increase[60, 120]] # 👉️ 50.0
Nếu chúng tôi gặp lỗi
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
5, chúng tôi sẽ trả về Infinity, tuy nhiên bạn có thể xử lý lỗi theo bất kỳ cách nào khác phù hợp với trường hợp sử dụng của bạn.Hàm thứ ba trong mẫu mã sử dụng toán tử modulo
5.Copied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
0Toán tử modulo [%] trả về phần còn lại từ sự phân chia giá trị thứ nhất cho phần thứ hai.
import numpy as np
ages = np.array[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
diff = ages[1:] - ages[:-1]
changes = diff / ages[1:] * 100
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
1Nếu giá trị ở phía bên phải bằng không, toán tử sẽ tăng ngoại lệ
import pandas as pd
ages = pd.Series[[20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9]]
changes = ages.pct_change[] * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
5.Các giá trị bên trái và bên phải cũng có thể là số điểm nổi.