Python 小抄

Python 備忘單

目錄

1.語法和空格
2.注釋
3.數字和運算
4.字符串處理
5.列表、元組和字典
6.JSON
7.循環
8.文件處理
9.函數
10.處理日期時間
11.NumPy
12.Pandas

要運行單元格,請按 Shift+Enter 或單擊頁面頂部的 Run(運行)。

1.語法和空格

Python 使用縮進空格來指示語句的級別。下面的單元格是一個示例,其中 ‘if’ 和 ‘else’ 處于同一級別,而 ‘print’ 由空格分隔到不同級別。相同級別的項目的間距應相同。

student_number = input("Enter your student number:")
if student_number != 0:print("Welcome student {}".format(student_number))
else:print("Try again!")
Enter your student number: 1Welcome student 1

2.注釋

在 Python 中,注釋以井號 ‘# ’ 開頭并延伸至該行的末尾。’# ’ 可以在行的開頭或代碼之后。

# 這是打印“hello world!”的代碼print("Hello world!") # 打印 hello world 語句
print("# 在本例中不是注釋")
Hello world!
# 在本例中不是注釋

3.數字和運算

與其他編程語言一樣,有四種類型的數字:

  • int 表示的整數(例如 1、20、45、1000)
  • float 表示的浮點數(例如 1.25、20.35、1000.00)
  • 長整數
  • 復數(例如 x+2y,其中 x 是已知的)
運算結果
x+yx 與 y 的和
x - yx 與 y 的差
x * yx 與 y 的乘積
x / yx 和 y 的商
x // yx 和 y 的商(取整)
x % yx / y 的余數
abs(x)x 的絕對值
int(x)將 x 轉換為整數
long(x)將 x 轉換為長整數
float(x)將 x 轉換為浮點
pow(x, y)x 的 y 次方
x ** yx 的 y 次方
# 數字示例
a = 5 + 8
print("Sum of int numbers: {} and number format is {}".format(a, type(a)))b = 5 + 2.3
print ("Sum of int and {} and number format is {}".format(b, type(b)))
Sum of int numbers: 13 and number format is <class 'int'>
Sum of int and 7.3 and number format is <class 'float'>

4.字符串處理

與其他編程語言一樣,Python 具有豐富的字符串處理功能。

# 將字符串存儲在變量中
test_word = "hello world to everyone"# 打印 test_word 值
print(test_word)# 使用 [] 訪問字符串的字符。第一個字符由 '0' 表示。
print(test_word[0])# 使用 len() 函數查找字符串的長度
print(len(test_word))# 在字符串中查找的一些示例
print(test_word.count('l')) # 計算 l 在字符串中重復出現的次數
print(test_word.find("o")) # 在字符串中查找字母 'o'。返回第一個匹配項的位置。
print(test_word.count(' ')) # 計算字符串中的空格數
print(test_word.upper()) # 將字符串更改為大寫
print(test_word.lower()) # 將字符串更改為小寫
print(test_word.replace("everyone","you")) # 將單詞“everyone”替換為“you”
print(test_word.title()) # 將字符串更改為標題格式
print(test_word + "!!!") # 連結字符串
print(":".join(test_word)) # 在每個字符之間添加“:”
print("".join(reversed(test_word))) # 將字符串進行反轉 
hello world to everyone
h
23
3
4
3
HELLO WORLD TO EVERYONE
hello world to everyone
hello world to you
Hello World To Everyone
hello world to everyone!!!
h:e:l:l:o: :w:o:r:l:d: :t:o: :e:v:e:r:y:o:n:e
enoyreve ot dlrow olleh

5.列表、元組和字典

Python 支持數據類型列表、元組、字典和數組。

列表

通過將所有項目(元素)放在方括號 [ ] 內并以逗號分隔來創建列表。列表可以具有任意數量的項目,并且它們可以具有不同的類型(整數、浮點數、字符串等)。

# Python 列表類似于數組。您也可以創建空列表。my_list = []first_list = [3, 5, 7, 10]
second_list = [1, 'python', 3]
# 嵌套多個列表
nested_list = [first_list, second_list]
nested_list
[[3, 5, 7, 10], [1, 'python', 3]]
# 合并多個列表
combined_list = first_list + second_list
combined_list
[3, 5, 7, 10, 1, 'python', 3]
# 您可以像分割字符串一樣分割列表
combined_list[0:3]
[3, 5, 7]
# 將新條目追加到列表
combined_list.append(600)
combined_list
[3, 5, 7, 10, 1, 'python', 3, 600]
# 從列表中刪除最后一個條目
combined_list.pop()
600
# 迭代列表
for item in combined_list:print(item)    
3
5
7
10
1
python
3

元組

元組類似于列表,但是您可以將其與括號 ( ) 一起使用,而不是與方括號一起使用。主要區別在于元組不可變,而列表可變。

my_tuple = (1, 2, 3, 4, 5)
my_tuple[1:4]
(2, 3, 4)

字典

字典也稱為關聯數組。字典由鍵值對的集合組成。每個鍵值對將鍵映射到其關聯值。

desk_location = {'jack': 123, 'joe': 234, 'hary': 543}
desk_location['jack']
123

6.JSON

JSON 是用 JavaScript 對象表示法編寫的文本。Python 有一個名為 json 的內置程序包,可用于處理 JSON 數據。

import json# 示例 JSON 數據
x = '{"first_name":"Jane", "last_name":"Doe", "age":25, "city":"Chicago"}'# 讀取 JSON 數據
y = json.loads(x)# 打印輸出結果,類似于字典
print("Employee name is "+ y["first_name"] + " " + y["last_name"])
Employee name is Jane Doe

7.循環

If, Else, ElIf 循環:和其他任何編程語言一樣,Python 支持條件語句。Python 依靠縮進(行的開頭是空格)來定義代碼范圍。

a = 22
b = 33
c = 100# if ... else 示例
if a > b:print("a is greater than b")
else:print("b is greater than a")# if .. else .. elif 示例if a > b:print("a is greater than b")
elif b > c:print("b is greater than c")
else:print("b is greater than a and c is greater than b")
b is greater than a
b is greater than a and c is greater than b

While 循環:只要條件為 true,就執行一組語句

# while 示例
i = 1
while i < 10:print("count is " + str(i))i += 1print("="*10)# 如果 x 為 2,則繼續進行下一個迭代。最后,條件為 false 時打印消息。x = 0
while x < 5:x += 1if x == 2:continueprint(x)
else:print("x is no longer less than 5")
count is 1
count is 2
count is 3
count is 4
count is 5
count is 6
count is 7
count is 8
count is 9
==========
1
3
4
5
x is no longer less than 5

For 循環: For 循環更像 Python 中的迭代器。For 循環用于遍歷序列(列表、元組、字典、集合、字符串或范圍)。

# 循環示例
fruits = ["orange", "banana", "apple", "grape", "cherry"]
for fruit in fruits:print(fruit)print("\n")
print("="*10)
print("\n")# 迭代范圍
for x in range(1, 10, 2):print(x)
else:print("task complete")print("\n")
print("="*10)
print("\n")# 迭代多個列表
traffic_lights = ["red", "yellow", "green"]
action = ["stop", "slow down", "go"]for light in traffic_lights:for task in action:print(light, task)
orange
banana
apple
grape
cherry

?

==========

?

1
3
5
7
9
task complete

?

==========

?

red stop
red slow down
red go
yellow stop
yellow slow down
yellow go
green stop
green slow down
green go

8.文件處理

在 Python 中處理文件的主要函數是 open() 函數。open() 函數使用兩個參數:filename 和 mode。

打開文件有四種不同的方法(模式):

  • “r” - 讀取
  • “a” - 追加
  • “w” - 寫入
  • “x” - 創建

此外,您還可以指定是以二進制還是文本模式處理文件。

  • “t” - 文本
  • “b” - 二進制
# 我們來創建一個測試文本文件
!echo "This is a test file with text in it.This is the first line." > test.txt
!echo "This is the second line." >> test.txt
!echo "This is the third line." >> test.txt
# 讀取文件
file = open('test.txt', 'r')
print(file.read())
file.close()print("\n")
print("="*10)
print("\n")# 讀取文件的前 10 個字符
file = open('test.txt', 'r')
print(file.read(10))
file.close()print("\n")
print("="*10)
print("\n")# 從文件中讀取行file = open('test.txt', 'r')
print(file.readline())
file.close()
This is a test file with text in it.This is the first line.
This is the second line.
This is the third line.

?
?

==========

?

This is a 

?

==========

?

This is a test file with text in it.This is the first line.
# 創建新文件file = open('test2.txt', 'w')
file.write("This is content in the new test2 file.")
file.close()# 讀取新文件的內容
file = open('test2.txt', 'r')
print(file.read())
file.close()
This is content in the new test2 file.
# 更新文件
file = open('test2.txt', 'a')
file.write("\nThis is additional content in the new file.")
file.close()# 讀取新文件的內容
file = open('test2.txt', 'r')
print(file.read())
file.close()
This is content in the new test2 file.
This is additional content in the new file.
# 刪除文件
import os
file_names = ["test.txt", "test2.txt"]
for item in file_names:if os.path.exists(item):os.remove(item)print(f"File {item} removed successfully!")else:print(f"{item} file does not exist.")
File test.txt removed successfully!
File test2.txt removed successfully!

9.函數

函數是在調用時運行的代碼塊。您可以將數據或 參數 傳遞到函數中。在 Python 中,函數是由 def 定義的。

# 定義函數
def new_funct():print("A simple function")# 調用函數
new_funct()
A simple function
# 帶有參數的示例函數def param_funct(first_name):print(f"Employee name is {first_name}.")param_funct("Harry")
param_funct("Larry")
param_funct("Shally")
Employee name is Harry.
Employee name is Larry.
Employee name is Shally.

匿名函數 (lambda):lambda 是一個小的匿名函數。Lambda 函數可以使用任意數量的參數,但只有一個表達式。

# lambda 示例
x = lambda y: y + 100
print(x(15))print("\n")
print("="*10)
print("\n")x = lambda a, b: a*b/100
print(x(2,4))
115

?

==========

?

0.08

10.處理日期時間

Python 中的 datetime 模塊可用于處理日期對象。

import datetimex = datetime.datetime.now()print(x)
print(x.year)
print(x.strftime("%A"))
print(x.strftime("%B"))
print(x.strftime("%d"))
print(x.strftime("%H:%M:%S %p"))
2024-05-15 12:42:35.994638
2024
Wednesday
May
15
12:42:35 PM

11.NumPy

NumPy 是使用 Python 進行科學計算的基本軟件包。以下是它包含的一部分內容:

  • 強大的 N 維數組對象
  • 復雜的(廣播)函數
  • 集成 C/C++ 和 Fortran 代碼的工具
  • 有用的線性代數、傅立葉變換和隨機數功能
# 使用 pip 安裝 NumPy
!pip install numpy
Requirement already satisfied: numpy in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (1.22.4)
# 導入 NumPy 模塊
import numpy as np

檢查您的數組

# 創建數組
a = np.arange(15).reshape(3, 5) # 在 3 x 5 維中創建范圍為 0-14 的數組
b = np.zeros((3,5)) # 使用 0 創建數組
c = np.ones( (2,3,4), dtype=np.int16 ) # 使用 1 創建數組并定義數據類型
d = np.ones((3,5))
a.shape # 數組維度
(3, 5)
len(b)# 數組長度
3
c.ndim # 數組維度的數量
3
a.size # 數組元素的數量
15
b.dtype # 數組元素的數據類型
dtype('float64')
c.dtype.name # 數據類型的名稱
'int16'
c.astype(float) # 將數組類型轉換為其他類型
array([[[1., 1., 1., 1.],[1., 1., 1., 1.],[1., 1., 1., 1.]],[[1., 1., 1., 1.],[1., 1., 1., 1.],[1., 1., 1., 1.]]])

基本數學運算

# 創建數組
a = np.arange(15).reshape(3, 5) # 在 3 x 5 維中創建范圍為 0-14 的數組
b = np.zeros((3,5)) # 使用 0 創建數組
c = np.ones( (2,3,4), dtype=np.int16 ) # 使用 1 創建數組并定義數據類型
d = np.ones((3,5))
np.add(a,b) # 加法
array([[ 0.,  1.,  2.,  3.,  4.],[ 5.,  6.,  7.,  8.,  9.],[10., 11., 12., 13., 14.]])
np.subtract(a,b) # 減法
array([[ 0.,  1.,  2.,  3.,  4.],[ 5.,  6.,  7.,  8.,  9.],[10., 11., 12., 13., 14.]])
np.divide(a,d) # 除法
array([[ 0.,  1.,  2.,  3.,  4.],[ 5.,  6.,  7.,  8.,  9.],[10., 11., 12., 13., 14.]])
np.multiply(a,d) # 乘法
array([[ 0.,  1.,  2.,  3.,  4.],[ 5.,  6.,  7.,  8.,  9.],[10., 11., 12., 13., 14.]])
np.array_equal(a,b) # 對比 - 數組方式
False

聚合函數

# 創建數組
a = np.arange(15).reshape(3, 5) # 在 3 x 5 維中創建范圍為 0-14 的數組
b = np.zeros((3,5)) # 使用 0 創建數組
c = np.ones( (2,3,4), dtype=np.int16 ) # 使用 1 創建數組并定義數據類型
d = np.ones((3,5))
a.sum() # 按數組求和
105
a.min() # 數組最小值
0
a.mean() # 數組平均值
7.0
a.max(axis=0) # 數組行的最大值
array([10, 11, 12, 13, 14])
np.std(a) # 標準差
4.320493798938574

子集、切片和索引

# 創建數組
a = np.arange(15).reshape(3, 5) # 在 3 x 5 維中創建范圍為 0-14 的數組
b = np.zeros((3,5)) # 使用 0 創建數組
c = np.ones( (2,3,4), dtype=np.int16 ) # 使用 1 創建數組并定義數據類型
d = np.ones((3,5))
a[1,2] # 選擇第 1 行、第 2 列的元素
7
a[0:2] # 選擇索引 0 和 1 上的項目
array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
a[:1] # 選擇第 0 行的所有項目
array([[0, 1, 2, 3, 4]])
a[-1:] # 選擇最后一行的所有項目
array([[10, 11, 12, 13, 14]])
a[a<2] # 從 'a' 中選擇小于 2 的元素
array([0, 1])

數組處理

# 創建數組
a = np.arange(15).reshape(3, 5) # 在 3 x 5 維中創建范圍為 0-14 的數組
b = np.zeros((3,5)) # 使用 0 創建數組
c = np.ones( (2,3,4), dtype=np.int16 ) # 使用 1 創建數組并定義數據類型
d = np.ones((3,5))
np.transpose(a) # 轉置數組 'a'
array([[ 0,  5, 10],[ 1,  6, 11],[ 2,  7, 12],[ 3,  8, 13],[ 4,  9, 14]])
a.ravel() # 展平數組
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
a.reshape(5,-2) # 重整但不更改數據
array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11],[12, 13, 14]])
np.append(a,b) # 將項目追加到數組
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,13., 14.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,0.,  0.,  0.,  0.])
np.concatenate((a,d), axis=0) # 連結數組
array([[ 0.,  1.,  2.,  3.,  4.],[ 5.,  6.,  7.,  8.,  9.],[10., 11., 12., 13., 14.],[ 1.,  1.,  1.,  1.,  1.],[ 1.,  1.,  1.,  1.,  1.],[ 1.,  1.,  1.,  1.,  1.]])
np.vsplit(a,3) # 在第 3 個索引處垂直拆分數組
[array([[0, 1, 2, 3, 4]]),array([[5, 6, 7, 8, 9]]),array([[10, 11, 12, 13, 14]])]
np.hsplit(a,5) # 在第 5 個索引處水平拆分數組
[array([[ 0],[ 5],[10]]),array([[ 1],[ 6],[11]]),array([[ 2],[ 7],[12]]),array([[ 3],[ 8],[13]]),array([[ 4],[ 9],[14]])]

Pandas

Pandas 是 BSD 許可的開源代碼庫,為 Python 編程語言提供了高性能、易于使用的數據結構和數據分析工具。

Pandas DataFrame 是 Python 中復雜數據集合在內存中使用最廣泛的表示形式。

# 使用 pip 安裝 pandas、xlrd 和 openpyxl
!pip install pandas
!pip install xlrd openpyxl
Requirement already satisfied: pandas in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (2.2.1)
Requirement already satisfied: numpy<2,>=1.22.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas) (1.22.4)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas) (2.9.0)
Requirement already satisfied: pytz>=2020.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas) (2024.1)
Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Collecting xlrdDownloading xlrd-2.0.1-py2.py3-none-any.whl.metadata (3.4 kB)
Requirement already satisfied: openpyxl in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (3.1.2)
Requirement already satisfied: et-xmlfile in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openpyxl) (1.1.0)
Downloading xlrd-2.0.1-py2.py3-none-any.whl (96 kB)
[2K   [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m96.5/96.5 kB[0m [31m11.2 MB/s[0m eta [36m0:00:00[0m
[?25hInstalling collected packages: xlrd
Successfully installed xlrd-2.0.1
# 導入 NumPy 和 Pandas 模塊
import numpy as np
import pandas as pd
# 示例 dataframe df
df = pd.DataFrame({'num_legs': [2, 4, np.nan, 0],'num_wings': [2, 0, 0, 0],'num_specimen_seen': [10, np.nan, 1, 8]},index=['falcon', 'dog', 'spider', 'fish'])
df # 顯示 dataframe df
num_legsnum_wingsnum_specimen_seen
falcon2.0210.0
dog4.00NaN
spiderNaN01.0
fish0.008.0
# 另一個示例 dataframe df1 - 使用帶有日期時間索引和標記列的 NumPy 數組
df1 = pd.date_range('20130101', periods=6)
df1 = pd.DataFrame(np.random.randn(6, 4), index=df1, columns=list('ABCD'))
df1 # 顯示 dataframe df1
ABCD
2013-01-010.4550052.0472800.260058-1.068430
2013-01-02-1.9038300.5212490.9067782.358446
2013-01-030.0362780.237705-0.836402-0.142862
2013-01-041.3021992.130269-0.467286-0.739326
2013-01-050.9240340.4136901.122296-1.917679
2013-01-06-1.4280251.2772790.1646011.313498

查看數據

df1 = pd.date_range('20130101', periods=6)
df1 = pd.DataFrame(np.random.randn(6, 4), index=df1, columns=list('ABCD'))
df1.head(2) # 查看頂部數據
ABCD
2013-01-010.9101310.8570311.3243970.768240
2013-01-02-1.1937120.598527-0.654860-1.528201
df1.tail(2) # 查看底部數據
ABCD
2013-01-051.009387-0.695923-1.2542390.374314
2013-01-06-0.6226980.9595860.3512941.240811
df1.index # 顯示索引列
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04','2013-01-05', '2013-01-06'],dtype='datetime64[ns]', freq='D')
df1.dtypes # 檢查數據類型
A    float64
B    float64
C    float64
D    float64
dtype: object
df1.describe() # 顯示數據的快速統計摘要
ABCD
count6.0000006.0000006.0000006.000000
mean-0.4321930.637821-0.1580000.080423
std1.1517990.7699160.9053030.973543
min-1.827255-0.695923-1.254239-1.528201
25%-1.1125370.500875-0.639878-0.299781
50%-0.7458560.727779-0.3572960.207468
75%0.5269240.9339480.2335540.669758
max1.0093871.6393821.3243971.240811

子集、切片和索引

df1 = pd.date_range('20130101', periods=6)
df1 = pd.DataFrame(np.random.randn(6, 4), index=df1, columns=list('ABCD'))
df1.T # 置換數據
2013-01-012013-01-022013-01-032013-01-042013-01-052013-01-06
A0.339706-0.033353-0.4699120.683896-0.119535-0.391874
B-1.2711341.1608610.594625-0.355716-1.718980-1.546150
C0.6312700.5258600.173641-1.885387-2.915834-0.781985
D0.674431-0.2748300.6303071.1326420.0216961.299410
df1.sort_index(axis=1, ascending=False) # 按軸排序
DCBA
2013-01-010.6744310.631270-1.2711340.339706
2013-01-02-0.2748300.5258601.160861-0.033353
2013-01-030.6303070.1736410.594625-0.469912
2013-01-041.132642-1.885387-0.3557160.683896
2013-01-050.021696-2.915834-1.718980-0.119535
2013-01-061.299410-0.781985-1.546150-0.391874
df1.sort_values(by='B') # 按值排序
ABCD
2013-01-05-0.119535-1.718980-2.9158340.021696
2013-01-06-0.391874-1.546150-0.7819851.299410
2013-01-010.339706-1.2711340.6312700.674431
2013-01-040.683896-0.355716-1.8853871.132642
2013-01-03-0.4699120.5946250.1736410.630307
2013-01-02-0.0333531.1608610.525860-0.274830
df1['A'] # 選擇列 A
2013-01-01    0.339706
2013-01-02   -0.033353
2013-01-03   -0.469912
2013-01-04    0.683896
2013-01-05   -0.119535
2013-01-06   -0.391874
Freq: D, Name: A, dtype: float64
df1[0:3] # 選擇索引 0 到 2
ABCD
2013-01-010.339706-1.2711340.6312700.674431
2013-01-02-0.0333531.1608610.525860-0.274830
2013-01-03-0.4699120.5946250.1736410.630307
df1['20130102':'20130104'] # 從匹配值的索引中選擇
ABCD
2013-01-02-0.0333531.1608610.525860-0.274830
2013-01-03-0.4699120.5946250.1736410.630307
2013-01-040.683896-0.355716-1.8853871.132642
df1.loc[:, ['A', 'B']] # 通過標簽在多軸上選擇
AB
2013-01-010.339706-1.271134
2013-01-02-0.0333531.160861
2013-01-03-0.4699120.594625
2013-01-040.683896-0.355716
2013-01-05-0.119535-1.718980
2013-01-06-0.391874-1.546150
df1.iloc[3] # 通過傳遞的整數的位置進行選擇
A    0.683896
B   -0.355716
C   -1.885387
D    1.132642
Name: 2013-01-04 00:00:00, dtype: float64
df1[df1 > 0] # 從滿足布爾運算條件的 DataFrame 中選擇值
ABCD
2013-01-010.339706NaN0.6312700.674431
2013-01-02NaN1.1608610.525860NaN
2013-01-03NaN0.5946250.1736410.630307
2013-01-040.683896NaNNaN1.132642
2013-01-05NaNNaNNaN0.021696
2013-01-06NaNNaNNaN1.299410
df2 = df1.copy() # 將 df1 數據集復制到 df2
df2['E'] = ['one', 'one', 'two', 'three', 'four', 'three'] # 添加帶有值的 E 列
df2[df2['E'].isin(['two', 'four'])] # 使用 isin 方法進行篩選
ABCDE
2013-01-03-0.4699120.5946250.1736410.630307two
2013-01-05-0.119535-1.718980-2.9158340.021696four

數據缺失

Pandas 主要使用值 np.nan 來表示缺失數據。默認情況下,它不包括在計算中。

df = pd.DataFrame({'num_legs': [2, 4, np.nan, 0],'num_wings': [2, 0, 0, 0],'num_specimen_seen': [10, np.nan, 1, 8]},index=['falcon', 'dog', 'spider', 'fish'])
df.dropna(how='any') # 刪除所有缺失數據的行
num_legsnum_wingsnum_specimen_seen
falcon2.0210.0
fish0.008.0
df.dropna(how='any', axis=1) # 刪除所有缺失數據的列
num_wings
falcon2
dog0
spider0
fish0
df.fillna(value=5) # 用值 5 填充缺失的數據
num_legsnum_wingsnum_specimen_seen
falcon2.0210.0
dog4.005.0
spider5.001.0
fish0.008.0
pd.isna(df) # 在缺失數據的位置獲取布爾掩碼
num_legsnum_wingsnum_specimen_seen
falconFalseFalseFalse
dogFalseFalseTrue
spiderTrueFalseFalse
fishFalseFalseFalse

文件處理

df = pd.DataFrame({'num_legs': [2, 4, np.nan, 0],'num_wings': [2, 0, 0, 0],'num_specimen_seen': [10, np.nan, 1, 8]},index=['falcon', 'dog', 'spider', 'fish'])
df.to_csv('foo.csv') # 寫入 CSV 文件
pd.read_csv('foo.csv') # 從 CSV 文件中讀取
Unnamed: 0num_legsnum_wingsnum_specimen_seen
0falcon2.0210.0
1dog4.00NaN
2spiderNaN01.0
3fish0.008.0
df.to_excel('foo.xlsx', sheet_name='Sheet1') # 寫入 Microsoft Excel 文件
pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA']) # 從 Microsoft Excel 文件中讀取
Unnamed: 0num_legsnum_wingsnum_specimen_seen
0falcon2.0210.0
1dog4.00NaN
2spiderNaN01.0
3fish0.008.0

繪圖

# 使用 pip 安裝 Matplotlib
!pip install matplotlib
Requirement already satisfied: matplotlib in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (3.8.3)
Requirement already satisfied: contourpy>=1.0.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (4.50.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (1.4.5)
Requirement already satisfied: numpy<2,>=1.21 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (1.22.4)
Requirement already satisfied: packaging>=20.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (21.3)
Requirement already satisfied: pillow>=8 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (10.2.0)
Requirement already satisfied: pyparsing>=2.3.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from matplotlib) (2.9.0)
Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
from matplotlib import pyplot as plt # 導入 Matplotlib 模塊
# 生成隨機時間序列數據
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000', periods=1000)) 
ts.head()
2000-01-01    0.273730
2000-01-02    0.934832
2000-01-03   -0.142245
2000-01-04   -0.499136
2000-01-05    0.169899
Freq: D, dtype: float64
ts = ts.cumsum()
ts.plot() # 繪制圖表
plt.show()

?
請添加圖片描述

?

# 在 DataFrame 上,plot() 方法可以方便繪制帶有標簽的所有列
df4 = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,columns=['A', 'B', 'C', 'D'])
df4 = df4.cumsum()
df4.head()
ABCD
2000-01-01-0.8477551.239531-0.7608560.668182
2000-01-02-1.1910671.930612-2.5876670.075473
2000-01-03-1.3537041.815771-1.788468-2.039681
2000-01-04-2.3381591.734058-2.269514-0.756332
2000-01-05-2.8355702.067088-3.3963661.352672
df4.plot()
plt.show()

?
請添加圖片描述

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/13195.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/13195.shtml
英文地址,請注明出處:http://en.pswp.cn/web/13195.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

三種方法進行跨服務器文件傳輸

今天需要在一個centOS服務器上編譯一個工具, 我的本地主機是ubuntu, 但是由于服務器是合規環境, 沒有文件傳輸的接口, 也不能訪問github等外網, 所以很多依賴只能下載到ubuntu然后在想辦法搞到服務器上. 這種場景有三種簡單有用的辦法, 整理一下. 方法一: 把主機配置成http ser…

6---Linux下版本控制器Git的知識點

一、Linux之父與Git的故事&#xff1a; Linux之父叫做“Linus Torvalds”&#xff0c;我們簡稱為雷納斯。Linux是開源項目&#xff0c;所以在Linux的早期開發中&#xff0c;許多世界各地的能力各異的程序員都參與到Linux的項目開發中。那時&#xff0c;雷納斯每天都會收到許許…

用ntpdate同步時間出現問題

1. 使用ntpdate同步 [rootnode ~]# ntpdate ntp.aliyun.com4 Aug 00:07:17 ntpdate[20924]: adjust time server 203.107.6.88 offset -0.001543 sec2. 查看時間 [rootnode ~]# date Thu Aug 4 00:07:46 CST 20223. 如果時間對不上 報錯信息 cna02:~ # ntpdate ntp1.aliyu…

mysql社區版最多支持多個連接并發

MySQL社區版對于并發連接數的支持并沒有一個固定的上限&#xff0c;它實際上取決于多個因素&#xff0c;包括服務器的硬件配置、MySQL的配置參數以及應用程序的設計等。 硬件配置&#xff1a;服務器的CPU、內存和磁盤I/O等硬件資源會直接影響MySQL可以處理的并發連接數。例如&a…

VMware Fusion 13.5.2 for Mac 發布,產品訂閱模式首個重大變更

VMware Fusion 13.5.2 for Mac 發布&#xff0c;產品訂閱模式首個重大變更 適用于基于 Intel 處理器和搭載 Apple 芯片的 Mac 的桌面虛擬化軟件 請訪問原文鏈接&#xff1a;https://sysin.org/blog/vmware-fusion-13/&#xff0c;查看最新版。原創作品&#xff0c;轉載請保留…

vue props接收組件數據(類型配置)

"props"接收的常見傳參類型有以下幾種&#xff1a;String&#xff1a;字符串類型&#xff0c;Number&#xff1a;數字類型&#xff0c;Boolean&#xff1a;布爾類型&#xff0c;Array&#xff1a;數組類型&#xff0c;Object&#xff1a;對象類型&#xff0c;Date&am…

文章解讀與仿真程序復現思路——中國電機工程學報EI\CSCD\北大核心《集裝箱海港級聯物流-能源耦合系統協同優化方法 》

本專欄欄目提供文章與程序復現思路&#xff0c;具體已有的論文與論文源程序可翻閱本博主免費的專欄欄目《論文與完整程序》 論文與完整源程序_電網論文源程序的博客-CSDN博客https://blog.csdn.net/liang674027206/category_12531414.html 電網論文源程序-CSDN博客電網論文源…

FPGA - GTX收發器-K碼 以及 IBERT IP核使用

一&#xff0c;前言 在FPGA - Xilinx系列高速收發器---GTX中詳細介紹了GTX的基礎知識&#xff0c;以及IP核的調用&#xff0c;下面將補充一下GTX在使用中的高速串行數據流在接收和發送時的控制與對齊&#xff08;K碼&#xff09;&#xff0c;以及高速接口GTX&#xff0c;如果G…

Springboot開發 -- Postman 調試 session 驗證 接口

當我們在開發Spring Boot應用時&#xff0c;經常會遇到帶有Session驗證的接口&#xff0c;這些接口需要用戶先登錄并獲取到Session ID&#xff08;或稱為cookie中的JSESSIONID&#xff09;&#xff0c;然后在后續的請求中攜帶這個Session ID來保持會話狀態。下面我將以一個實際…

力扣72-編輯距離

題目鏈接 記憶化搜索&#xff1a; 解題關鍵&#xff1a;每次僅考慮兩字符串word1、word2分別從0 - i修改成0-j下標的完全匹配(下標表示&#xff09; 臨界條件&#xff1a;當 i 或 j 小于0時&#xff0c;表示該字符串為空&#xff0c;編輯距離確定為 y1 或 x1 int dp[501][501…

Hello, GPT-4o!

2024年5月13日&#xff0c;OpenAI 在官網正式發布了最新的旗艦模型 GPT-4o 它是一個 多模態模型&#xff0c;可以實時推理音頻、視頻和文本。 * 發布會完整版視頻回顧&#xff1a;https://www.youtube.com/watch?vDQacCB9tDaw GPT-4o&#xff08;“o”代表“omni”&#xff0c…

高效協同,智慧繪制:革新型流程圖工具全解析

流程圖&#xff0c;作為一種直觀展示工作過程和系統運作的工具&#xff0c;在現代辦公和項目管理中發揮著不可或缺的作用。 其優勢在于能夠清晰、直觀地呈現復雜的過程和關系&#xff0c;幫助人們快速理解并掌握關鍵信息。同時&#xff0c;流程圖也廣泛應用于各種場景&#xf…

linux常用命令(持續更新)

1.sudo -i 切換root權限 2. ll 和 ls 查看文件夾下面的文件 3. cat 查看文件內容 cat xxx.txt |grep 好 篩選出有好的內容 4. vi 編輯文件 點擊insert進入編輯模式 編輯完之后點擊Esc退出編輯模式 數據:wq!回車保存文件 5. ssh 連接到可以訪問的系統 6. telnet 看端口是否可以…

【Python】圖像批量合成視頻,并以文件夾名稱命名合成的視頻

一個文件夾中有多個子文件夾&#xff0c;子文件夾中有多張圖像。如何把批量把子文件夾中的圖像合成視頻&#xff0c;視頻名稱是子文件夾的名稱&#xff0c;生成的視頻保存到指定文件夾&#xff0c;效果記錄。 代碼 import os import cv2def create_video_from_images(image_f…

leetcode刷題(6):二叉樹的使用

文章目錄 104. 二叉樹的最大深度解題思路c 實現 94. 二叉樹的中序遍歷解題思路c 實現 101. 對稱二叉樹解題思路c 實現 96. 不同的二叉搜索樹解題思路c 實現 102. 二叉樹的層序遍歷解題思路c 實現 104. 二叉樹的最大深度 題目: 給定一個二叉樹 root &#xff0c;返回其最大深度…

重新認識Flutter跨平臺技術(上)

背景 2017年,Flutter剛推出來的時候,正好自己在做TV Launcher開發的工作。 我們知道TV Launcher是Android TV操作系統中的一個啟動器應用程序。它負責在打開電視時展示給用戶的主要界面,包括應用程序圖標、推薦內容等。通過Android TV Launcher,用戶可以方便地瀏覽和啟動…

ALV 圖標顯示

前言 在ABAP ALV中&#xff0c;使用fieldcat來定義列表中每個字段的顯示屬性&#xff0c;包括圖標&#xff08;Icon&#xff09;的顯示。圖標可以在ALV列表中為特定列的行或標題添加圖形元素&#xff0c;以增強視覺提示或傳達附加信息。 ICON查詢 圖標的名稱用事務碼”ICON“進…

智能BI(后端)-- 系統異步化

文章目錄 系統問題分析什么是異步化&#xff1f;業務流程分析標準異步化的業務流程系統業務流程 線程池為什么需要線程池&#xff1f;線程池兩種實現方式線程池的參數線程池的開發 項目異步化改造 系統問題分析 問題場景&#xff1a;調用的服務能力有限&#xff0c;或者接口的…

離岸公司+外貿

為什么外貿公司老板都喜歡注冊離岸公司呢&#xff1f;怎樣利用離岸公司做進出口貿易呢&#xff1f; 今天大家花一分鐘時間來了解清楚 第一步就是注冊一家離岸公司&#xff0c;將這個離岸公司作為國際外貿的中轉站&#xff0c;與國外客戶簽訂單&#xff0c;你從國內工廠采購商…

【文檔理解】TextMonkey:一種OCR-Free的用于文檔理解的多模態大模型

背景 傳統的信息提取&#xff0c;通常是從文本中提取信息&#xff0c;相關技術也比較成熟。然而對于復雜領域&#xff0c;例如圖片&#xff0c;文檔等形式的數據&#xff0c;想要提取出高質量的、可信的數據難度就比較大了&#xff0c;這種任務也常稱為&#xff1a;視覺文檔理…