Hướng dẫn how do you calculate percentage increase or decrease in python? - làm thế nào để bạn tính toán phần trăm tăng hoặc giảm trong python?

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:

  1. Sử dụng toán tử phân chia
    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]
    
    2 để chia một số cho một số khác.
  2. Nhân số chỉ số với
    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 để có được tỷ lệ phần trăm.
  3. 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:

TênSự mô tả
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]
8
con số đến vòng đến độ chính xá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]
9 sau 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]
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

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
1 trả về số được làm tròn đến độ chính xác 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]
9 sau điểm thập phân.

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ừ

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
0 lê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]
3 là

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
2.

Nếu bạn luôn cần có một số dương, hãy sử dụng hàm

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
3.

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

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
5.

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]
0

Toá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]
1

Nế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.

Làm thế nào để bạn tìm thấy tỷ lệ phần trăm tăng hoặc giảm trong Python?

Sử dụng bộ phận / toán tử để chia một số cho một số khác. Một số chỉ số cho số 100 để có được tỷ lệ phần trăm.Kết quả cho thấy bao nhiêu phần trăm số đầu tiên của số thứ hai. Multiply the quotient by 100 to get the percentage. The result shows what percent the first number is of the second.

Có chức năng phần trăm trong Python không?

Không có nhà điều hành như vậy trong Python, nhưng nó là tầm thường để tự mình thực hiện.Trong thực tế trong điện toán, tỷ lệ phần trăm gần như không hữu ích như một modulo, vì vậy không có ngôn ngữ nào mà tôi có thể nghĩ về việc thực hiện một., but it is trivial to implement on your own. In practice in computing, percentages are not nearly as useful as a modulo, so no language that I can think of implements one.

Làm thế nào để bạn viết tỷ lệ phần trăm trong Python?

Để in một giá trị phần trăm trong Python, hãy sử dụng str.Định dạng () phương thức hoặc chuỗi f trên mẫu ngôn ngữ định dạng "{:. 0%}" ...
your_number = 0,42 ..
Tỷ lệ phần trăm = "{:. 0%}".Định dạng (your_number).
print(percentage).

Làm thế nào để bạn tính toán tỷ lệ phần trăm của từng phần tử trong danh sách Python?

Trong đó, chúng tôi xây dựng danh sách các phần tử tích cực bằng cách sử dụng danh sách hiểu và sau đó tính độ dài của danh sách bằng Len (), cả hai độ dài được chia và nhân với 100 để nhận phần trăm.construct positive elements list using list comprehension and then compute the length of lists using len(), both lengths are divided and multiplied by 100 to get percentage count.