Back to Study Material Python

Pandas

Pandas is Python's go-to library for data manipulation and analysis. Its two core structures are the Series (a labeled 1-D array) and the DataFrame (a labe...

Pandas

Working with Pandas DataFrames

Pandas is Python's go-to library for data manipulation and analysis. Its two core structures are the Series (a labeled 1-D array) and the DataFrame (a labeled 2-D table), both built on top of NumPy arrays.

Install with pip install pandas and import as pd: import pandas as pd.

Creating a DataFrame: data = {'Name': ['Alice', 'Bob', 'Carol'], 'Score': [88, 92, 79]} df = pd.DataFrame(data)

Reading real-world data is just as simple: df = pd.read_csv('students.csv') # CSV file df = pd.read_excel('report.xlsx') # Excel file df = pd.read_json('data.json') # JSON file

Exploration methods every analyst uses daily: df.head() shows the first five rows, df.info() lists column types and null counts, df.describe() computes statistics for numeric columns.

Selecting data: df['Score'] returns a Series, df[['Name', 'Score']] returns a DataFrame. For row and column selection together, use df.loc[0:2, 'Name':'Score'] (label-based) or df.iloc[0:3, 0:2] (position-based).

Filtering rows uses boolean indexing: df[df['Score'] > 80] returns only rows where Score exceeds 80.

GroupBy is essential for aggregation: df.groupby('Department')['Score'].mean() computes the average score per department in one line.

Handling missing values: df.isnull().sum() counts NaN values per column. df.fillna(0) replaces NaN with zero; df.dropna() removes rows containing any NaN.

Merging DataFrames resembles SQL joins: pd.merge(df1, df2, on='id', how='left') performs a left join on the 'id' column.

Practical tip: Chain operations using method chaining to keep code readable: result = (df.dropna().groupby('City')['Revenue'].sum().reset_index().sort_values('Revenue', ascending=False))

Exam tip: Know the difference between loc (label-based, inclusive end) and iloc (integer-based, exclusive end). This distinction appears frequently in technical interviews.