Pandas Cheat Sheet

AIVILLAIN·2023년 3월 8일
0

Importing pandas

import pandas as pd

Reading data

CSV files

df = pd.read_csv('file.csv')

Excel files

df = pd.read_excel('file.xlsx')

Basic operations

df.head()

Tail

df.tail()

Info

df.info()

Describe

df.describe()

Shape

df.shape

Columns

df.columns

Index

df.index

Sorting

df.sort_values(by='column_name', ascending=False)

Selecting data

Columns

df['column_name']

Rows

df.loc[row_label]
df.iloc[row_number]

Filtering

df[df['column_name'] == value]

Aggregating data

Group by

df.groupby('column_name').mean()

Pivot table

pd.pivot_table(df, values='value', index='index_column', columns='columns_to_show')

Aggregating

df['column_name'].agg(['sum', 'mean', 'min', 'max'])

Cleaning data

Renaming columns

df.rename(columns={'old_name': 'new_name'})

Dropping columns

df.drop(columns=['column_name'])

Filling missing values

df.fillna(value)

Dropping missing values

df.dropna()

Merging data

merged_df = pd.merge(df1, df2, on='column_name')

Exporting data

CSV files

df.to_csv('file.csv', index=False)

Excel files

df.to_excel('file.xlsx', index=False)
profile
소신있는 오픈마인드

0개의 댓글