Pandas DataFrame Methods
1. Basic DataFrame Methods
Method Description
df.head(n) Returns the first n rows (default is 5).
df.tail(n) Returns the last n rows (default is 5).
df.shape Returns a tuple representing the dimensions of the DataFrame.
df.info() Provides a summary of the DataFrame.
df.describe() Generates descriptive statistics for numeric columns.
2. Data Manipulation Methods
Method Description
df.drop() Drops specified rows or columns.
df.rename() Renames columns or index labels.
df.sort_values() Sorts the DataFrame by one or more columns.
df.groupby() Groups data by one or more columns for aggregation.
3. Data Cleaning Methods
Method Description
df.fillna() Fills missing values with a specified value or method.
df.dropna() Drops rows or columns with missing values.
df.replace() Replaces specified values with new values.
df.drop_duplicates() Removes duplicate rows.
4. Aggregation and Computation Methods
Method Description
df.sum() Computes the sum of values for each column or row.
df.mean() Computes the mean of values for each column or row.
df.median() Computes the median of values for each column or row.
df.count() Counts the number of non-missing values for each column or row.
5. Selection and Filtering Methods
Method Description
df.loc[] Selects rows and columns by label.
df.iloc[] Selects rows and columns by integer position.
df.query() Filters rows using a query expression.
6. Transformation Methods
Method Description
df.apply() Applies a function to each row or column.
df.applymap() Applies a function element-wise to the entire DataFrame.
7. Input/Output Methods
Method Description
df.to_csv() Writes the DataFrame to a CSV file.
df.to_excel() Writes the DataFrame to an Excel file.
df.read_csv() Reads a CSV file into a DataFrame.
df.read_json() Reads a JSON file into a DataFrame.
8. Visualization Methods
Method Description
df.plot() Creates plots (e.g., line, bar, scatter) from the DataFrame.
df.hist() Creates histograms for numeric columns.
df.boxplot() Creates boxplots for numeric columns.
Example Usage
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [50000, 60000, 70000]
}
df = pd.DataFrame(data)
print(df.head()) # Display first 5 rows
print(df.describe()) # Summary statistics
print(df['Age'].mean()) # Calculate mean of 'Age' column
df.fillna(0, inplace=True) # Fill missing values with 0
Key Points
- Methods are functions that operate on DataFrames or Series.
- Some methods modify the DataFrame in place (e.g., fillna(inplace=True)), while others return a new
object.
- Methods are essential for data manipulation, cleaning, aggregation, and analysis.