15個(gè)高效的Pandas代碼片段
Python的Pandas庫是數(shù)據(jù)科學(xué)家必備的基礎(chǔ)工具,在本文中,我們將整理15個(gè)高級Pandas代碼片段,這些代碼片段將幫助你簡化數(shù)據(jù)分析任務(wù),并從數(shù)據(jù)集中提取有價(jià)值的見解。

過濾數(shù)據(jù)
import pandas as pd
 
 # Create a DataFrame
 data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40]}
 
 df = pd.DataFrame(data)
 
 # Filter rows where Age is greater than 30
 filtered_df = df[df['Age'] > 30]
 print(filtered_df)分組和聚合數(shù)據(jù)
# Grouping by a column and calculating the mean
 grouped = df.groupby('Age').mean()
 print(grouped)處理缺失數(shù)據(jù)
# Check for missing values
 missing_values = df.isnull().sum()
 
 
 # Fill missing values with a specific value
 df['Age'].fillna(0, inplace=True)將函數(shù)應(yīng)用于列
# Applying a custom function to a column
 df['Age'] = df['Age'].apply(lambda x: x * 2)連接DataFrames
# Concatenate two DataFrames
 df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', 'B1']})
 df2 = pd.DataFrame({'A': ['A2', 'A3'], 'B': ['B2', 'B3']})
 
 
 result = pd.concat([df1, df2], ignore_index=True)
 print(result)合并DataFrames
# Merge two DataFrames
 left = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
 right = pd.DataFrame({'key': ['B', 'C', 'D'], 'value': [4, 5, 6]})
 
 merged = pd.merge(left, right, notallow='key', how='inner')
 print(merged)數(shù)據(jù)透視表
# Creating a pivot table
 pivot_table = df.pivot_table(index='Name', columns='Age', values='Value')
 print(pivot_table)處理日期時(shí)間數(shù)據(jù)
# Converting a column to DateTime
 df['Date'] = pd.to_datetime(df['Date'])數(shù)據(jù)重塑
# Melting a DataFrame
 melted_df = pd.melt(df, id_vars=['Name'], value_vars=['A', 'B'])
 print(melted_df)使用分類數(shù)據(jù)類型
# Encoding categorical variables
 df['Category'] = df['Category'].astype('category')
 df['Category'] = df['Category'].cat.codes數(shù)據(jù)采樣
# Randomly sample rows from a DataFrame
 sampled_df = df.sample(n=2)計(jì)算累計(jì)和
# Calculating cumulative sum
 df['Cumulative_Sum'] = df['Values'].cumsum()刪除重復(fù)項(xiàng)
# Removing duplicate rows
 df.drop_duplicates(subset=['Column1', 'Column2'], keep='first', inplace=True)快捷進(jìn)行onehot編碼
dummy_df = pd.get_dummies(df, columns=['Category'])導(dǎo)出數(shù)據(jù)
df.to_csv('output.csv', index=False)為什么要加上導(dǎo)出數(shù)據(jù)呢?,因?yàn)樵趯?dǎo)出數(shù)據(jù)時(shí)一定要加上index=False參數(shù),這樣才不會將pandas的索引導(dǎo)出到csv中。
總結(jié)
這15個(gè)Pandas代碼片段將大大增強(qiáng)您作為數(shù)據(jù)科學(xué)家的數(shù)據(jù)操作和分析能力。將它們整合到的工作流程中,可以提高處理和探索數(shù)據(jù)集的效率和效率。















 
 
 











 
 
 
 