自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Pandas入門的12個技巧

開發(fā) 前端
Pandas 是一個非常流行的 Python 庫,它提供了強大、靈活和高性能的數(shù)據(jù)處理工具。

今天,就為大家總結一下 “Pandas數(shù)據(jù)處理的12技巧”,拿來即用,隨查隨用。

今天,就為大家總結一下 “Pandas數(shù)據(jù)處理的12技巧”,拿來即用,隨查隨用。

1. 安裝

你還可以使用內置的 Python 工具 pip 安裝 Pandas 并運行以下命令:

$ pip install pandas

安裝完成后的提示成功,則可以環(huán)境中使用pandas包了。

import pandas

2. 創(chuàng)建數(shù)據(jù)列

Pandas一維數(shù)組(數(shù)據(jù)列)可以保存任何數(shù)據(jù)類型。一般通過調用 pd.Series() 方法實現(xiàn),不指定index,默認為0,1,2,3...。

import pandas
import numpy

S = pandas.Series(
    [1, 2, 3, 4],  # 數(shù)據(jù)
    index=["a", "b", "c", "d"],  # 指定索引
    dtype=numpy.int8,  # 指定數(shù)據(jù)類型
)
S.name = "test"  # 創(chuàng)建一維數(shù)組的名稱
S.index.name = "index"  # 創(chuàng)建一維數(shù)組的索引名稱
print(S)

輸出:

index
a    1
b    2
c    3
d    4
Name: test, dtype: int8

3. 創(chuàng)建數(shù)據(jù)框

創(chuàng)建具有列的二維數(shù)據(jù)結構的對象(數(shù)據(jù)框)。一般通過調用 pd.DataFrame() 方法實現(xiàn),不指定index,默認為0,1,2,3...。

import pandas

dat_list = [
    [1, 2, 3],
    [4, 6, 8],
    [10, 11, 12],
]
df = pandas.DataFrame(
    dat_list,
    index=["i1", "i3", "i2"],
    columns=["a", "b", "c"],
)
print(df)

輸出:

a   b   c
i1   1   2   3
i3   4   6   8
i2  10  11  12

4. CSV文件的讀寫

Pandas 支持從 CSV的讀寫,我們用 pd.read_csv() 和 pd.to_csv() 方法來實現(xiàn)。

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df.to_csv("output/test.csv") # 導出到CSV文件
dfnew = pandas.read_csv("output/test.csv", index_col=0) # index_col=0 表示第一列為索引
print(dfnew)

5. Excel文件的讀寫

Pandas 支持從 Excel的讀寫,我們用 pd.read_excel() 和 pd.to_excel() 方法來實現(xiàn)。

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df.to_excel("output/test.xlsx") # 導出到excel
dfnew = pandas.read_excel("output/test.xlsx", index_col=0) # index_col=0 表示第一列為索引
print(dfnew)

6. 通過位置選擇值

Pandas 支持通過位置選擇值,我們用 df.iloc[row_index, column_index] 來實現(xiàn)。

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)

print(df.iloc[2, 1])  # 6 結果為一個值
print(df.iloc[[2], [1]])  # 結果為一個元素的數(shù)據(jù)框
print(df.iloc[1:3, :])  # 結果為一個數(shù)據(jù)框

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
6
    B
i3  6
    A  B  C   D
i1  2  5  8  11
i3  3  6  9  12

7. 通過名稱選擇值

Pandas 支持通過名稱選擇值,我們用 df.loc[row_name, column_name] 來實現(xiàn)。

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)

print(df.loc["i2", "C"])  # 6 結果為一個值
print(df.loc[["i2"], ["C"]])  # 結果為一個元素的數(shù)據(jù)框
print(df.loc[["i2", "i3"], ["A", "D"]])  # 切片

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
7
    C
i2  7
    A   D
i2  1  10
i3  3  12

8. 標簽可以重復么?

這是一個有意思的問題,如果重復了如何取值呢,如何去掉重復呢?

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i2"])
print(df)
print(df.loc["i2", "C"])
df.columns = ["A", "B", "B", "D"]
print(df)
print(df.loc["i2", "B"])

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i2  3  6  9  12
i2    7
i2    9
Name: C, dtype: int64
    A  B  B   D
i2  1  4  7  10
i1  2  5  8  11
i2  3  6  9  12
    B  B
i2  4  7
i2  6  9

9. 刪除行/列

Pandas 支持刪除行/列,我們用 df.drop([col/row name], axis=0/1) 來實現(xiàn)。

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df.drop(
    ["i2", "i1"],
    axis=0,  # axis=0刪除行,axis=1刪除列
    inplace=True, # 如果為True,則在原數(shù)據(jù)上進行操作,否則,創(chuàng)建一個新的數(shù)據(jù)對象
    errors="ignore",  # 忽略不存在的列
)
print(df)

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
    A  B  C   D
i3  3  6  9  12

10. 在最后增加列

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12]
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df['E'] = 0
print(df)

輸出為:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
    A  B  C   D  E
i2  1  4  7  10  0
i1  2  5  8  11  0
i3  3  6  9  12  0

11. 在最后增加行

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12],
}
df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df = df.append(
    pandas.DataFrame({"A": 13, "B": 14, "C": 15, "D": 16}, index=["i4"]),
    ignore_index=True,
)
print(df)

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
    A   B   C   D
0   1   4   7  10
1   2   5   8  11
2   3   6   9  12
3  13  14  15  16

12. 通過標簽(索引或列名)排序

import pandas

d = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9],
    "D": [10, 11, 12]
}

df = pandas.DataFrame(d, index=["i2", "i1", "i3"])
print(df)
df = df.sort_index(
    axis=0, # axis=0 按行標簽排序,axis=1 按列標簽排序
    level=None,
    ascending=True, # 是否升序
    inplace=False, # 是否修改原數(shù)據(jù)
    kind="quicksort", # 排序算法
    na_positinotallow="last", # 如果有NA值,放在最后
    sort_remaining=True, # 是否排序剩余列
)
print(df)

輸出:

A  B  C   D
i2  1  4  7  10
i1  2  5  8  11
i3  3  6  9  12
    A  B  C   D
i1  2  5  8  11
i2  1  4  7  10
i3  3  6  9  12

總結

上面分享的pandas入門的12個技巧,希望對你有所幫助。

責任編輯:華軒 來源: 哈希編程
相關推薦

2020-08-14 10:57:49

開發(fā)技能代碼

2020-03-10 08:55:50

PandasNumPy函數(shù)

2021-07-07 09:50:23

NumpyPandasPython

2024-07-26 00:35:33

2022-11-04 15:56:41

2023-12-19 13:31:00

CSS前端技巧

2024-02-26 08:20:00

CSS開發(fā)

2023-03-24 16:41:36

Pandas技巧數(shù)據(jù)處理

2022-05-10 09:33:50

Pandas技巧代碼

2019-11-20 10:54:32

Python數(shù)據(jù)結構數(shù)據(jù)庫

2022-10-19 15:20:58

pandas數(shù)據(jù)處理庫技巧

2023-07-24 15:24:00

前端CSS 技巧

2015-08-04 10:51:26

vim效率技巧

2022-03-10 08:01:06

CSS技巧選擇器

2025-01-14 00:01:01

2020-06-23 08:28:26

前端開發(fā)技巧

2022-11-24 10:34:05

CSS前端

2024-06-11 00:09:00

JavaScript模式變量

2016-05-10 10:16:13

JavaScript技巧

2024-05-28 14:45:00

點贊
收藏

51CTO技術棧公眾號