🐍 Bitmuncher's Python Stuff 🐍



Python Pandas Cheatsheet



Reading and Writing Data

pd.read_csv('file.csv') Read a CSV file into a DataFrame
df.to_csv('file.csv') Write a DataFrame to a CSV file
pd.read_excel('file.xlsx') Read an Excel file into a DataFrame
df.to_excel('file.xlsx') Write a DataFrame to an Excel file

Data Inspection

df.head() Get the first 5 rows of a DataFrame
df.tail() Get the last 5 rows of a DataFrame
df.info() Get information about a DataFrame, including data types and memory usage
df.describe() Get summary statistics of numerical columns in a DataFrame

Data Selection

df[col] Get a single column by name as a Series
df[[col1, col2]] Get multiple columns by name as a DataFrame
df.loc[row, col] Get a single value by row and column label
df.iloc[row, col] Get a single value by row and column index

Data Manipulation

df['new_col'] = value Add a new column to a DataFrame (or change existing value)
df.drop(col, axis=1, inplace=True) Remove/drop a column from a DataFrame
df.drop(row, axis=0, inplace=True) Remove/drop a row from a DataFrame
df.sort_values(by=col, ascending=True) Sort a DataFrame by a column

Grouping and Aggregation

df.groupby(col).sum() Group a DataFrame by a column and compute the sum of each group
df.groupby(col).median() Group a DataFrame by a column and compute the median of each group
df.groupby(col).max() Group a DataFrame by a column and compute the maximum of each group
df.groupby(col).first() Group a DataFrame by a column and return the first row of each group
df.groupby(col).size() Group a DataFrame by a column and return the size of each group
df.groupby(col).agg(func) Group a DataFrame by a column and apply a specific aggregation function to each group


  
Design based on Dracula UI