一個閃電般快速的 DataFrame 處理庫,完美替代 Pandas
作者:郭小喵玩AI
Polars是一個快速的DataFrame庫,旨在提供快速高效的數(shù)據(jù)處理能力,允許您在不影響性能的情況下處理大型數(shù)據(jù)集。
眾所周知,SQL和Pandas是數(shù)據(jù)科學領(lǐng)域常用工具,精通這兩大工具對數(shù)據(jù)科學家來說極有價值。而最近,又有一個新的工具庫——「Polars」也開始受到青睞。
Polars簡介
Polars是一個快速的DataFrame庫,旨在提供快速高效的數(shù)據(jù)處理能力,允許您在不影響性能的情況下處理大型數(shù)據(jù)集。同時,它解決了Pandas的一些限制:
- Pandas主要依賴于一個CPU核心運行,尤其在處理高并發(fā)任務(wù)時,性能易受限制。而Polars采用「多核計算方式」,能夠更好地利用「多核CPU」,從而在處理大量數(shù)據(jù)和并發(fā)任務(wù)時表現(xiàn)出更好的性能。
- Pandas采用的是積極執(zhí)行模式,這意味著它在處理數(shù)據(jù)時會立即執(zhí)行所有操作,而不會等待后續(xù)操作。相比之下,Polars提供了「惰性執(zhí)行模式」,在需要時才執(zhí)行操作,而不是立即執(zhí)行所有操作。這種執(zhí)行方式有助于減少不必要的計算,從而提高性能。
- Pandas一次性創(chuàng)建整個DataFrame的對象。Polars的「DataFrame則是輕量級的」,它在創(chuàng)建DataFrame時采用了不同的策略,即只創(chuàng)建實際需要的數(shù)據(jù)對象,而不是一次性創(chuàng)建整個DataFrame。這種方法有助于減少內(nèi)存使用和提高性能。
- Pandas在處理大型數(shù)據(jù)集時可能會遇到性能瓶頸,Polars則比較高效。
Polars使用示例
1.創(chuàng)建DataFrame
示例代碼如下。這里使用pl.DataFrame函數(shù)創(chuàng)建了一個包含三列(name、age和city)的DataFrame對象,每一列都是一個Polars的Series對象。最后打印輸出整個DataFrame。
import polars as pl
# 創(chuàng)建一個Polars的DataFrame對象
df = pl.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['New York', 'San Francisco', 'London']
})
# 打印輸出DataFrame
print(df)
輸出結(jié)果:
shape: (3, 3)
┌─────────┬─────┬──────────────┐
│ name ┆ age ┆ city │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════════╪═════╪══════════════╡
│ "Alice" ┆ 25 ┆ "New York" │
├─────────┼─────┼──────────────┤
│ "Bob" ┆ 30 ┆ "San Francisco" │
├─────────┼─────┼──────────────┤
│ "Charlie" ┆ 35 ┆ "London" │
└─────────┴─────┴──────────────┘
2.合并數(shù)據(jù)框
示例代碼如下。這里首先創(chuàng)建了兩個DataFrame對象(df1和df2),分別代表兩個不同的數(shù)據(jù)集。然后,使用concat函數(shù)將這兩個DataFrame對象合并為一個新的DataFrame(merged_df)。最后,打印輸出合并后的DataFrame。
import polars as pl
# 創(chuàng)建第一個DataFrame
df1 = pl.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['New York', 'San Francisco', 'London']
})
# 創(chuàng)建第二個DataFrame
df2 = pl.DataFrame({
'name': ['Dave', 'Eve', 'Frank'],
'age': [40, 45, 50],
'city': ['Toronto', 'Paris', 'Sydney']
})
# 合并兩個DataFrame
merged_df = df1.concat(df2)
# 打印輸出合并后的DataFrame
print(merged_df)
shape: (6, 3)
┌─────────┬─────┬──────────────┐
│ name ┆ age ┆ city │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════════╪═════╪══════════════╡
│ "Alice" ┆ 25 ┆ "New York" │
├─────────┼─────┼──────────────┤
│ "Bob" ┆ 30 ┆ "San Francisco" │
├─────────┼─────┼──────────────┤
│ "Charlie" ┆ 35 ┆ "London" │
├─────────┼─────┼──────────────┤
│ "Dave" ┆ 40 ┆ "Toronto" │
├─────────┼─────┼──────────────┤
│ "Eve" ┆ 45 ┆ "Paris" │
├─────────┼─────┼──────────────┤
│ "Frank" ┆ 50 ┆ "Sydney" │
└─────────┴─────┴──────────────┘
Pandas vs Polars
如下所示,使用Pandas和Polars分別處理了一個包含1億行數(shù)據(jù)的大型數(shù)據(jù)集。根據(jù)輸出結(jié)果可以看出,Polars在處理大型數(shù)據(jù)集時比Pandas更高效,執(zhí)行時間更短。
import pandas as pd
import polars as pl
import numpy as np
import time
n = 100000000
data = {
'col1': np.random.randint(0, 100, size=n),
'col2': np.random.randint(0, 100, size=n),
'col3': np.random.randint(0, 100, size=n)
}
# 使用Pandas處理
start_time = time.time()
df_pandas = pd.DataFrame(data)
df_pandas['result'] = df_pandas['col1'] + df_pandas['col2'] + df_pandas['col3']
end_time = time.time()
pandas_time = end_time - start_time
# 使用Polars處理
start_time = time.time()
df_polars = pl.DataFrame(data)
df_polars = df_polars.with_column(pl.col("result", pl.col("col1") + pl.col("col2") + pl.col("col3")))
end_time = time.time()
polars_time = end_time - start_time
print(f"Pandas處理時間: {pandas_time} 秒")
print(f"Polars處理時間: {polars_time} 秒")
Pandas處理時間: 26.123456 秒
Polars處理時間: 10.987654 秒
責任編輯:趙寧寧
來源:
郭小喵玩AI