Pandas
Python Data Analysis
A Python data analysis library. Manipulates tabular data with DataFrames. A replacement for Excel.
Python Data Analysis
A Python data analysis library. Manipulates tabular data with DataFrames. A replacement for Excel.
Pandas is a Python library developed by Wes McKinney in 2008 for financial data analysis. With its DataFrame and Series data structures, it lets you manipulate tabular data intuitively, offering the automation and scalability of programming while handling data much like a spreadsheet.
A DataFrame is a 2-dimensional table made up of rows (index) and columns (column), and a Series is a 1-dimensional array. You can perform filtering, grouping, and joins as in SQL, and use loc/iloc for label-based/position-based indexing. Method chaining lets you connect multiple operations in a single line, which keeps the code concise.
It can read and write various formats such as CSV, Excel, JSON, SQL, and Parquet, and performs complex data transformations with groupby, pivot_table, merge, and concat. It also offers rich convenience features for handling missing values (fillna, dropna), string processing (str accessor), and date processing (dt accessor).
With large datasets (10GB or more), memory limits apply, so you switch to Polars, Dask, or Spark. However, for small-to-medium-scale data analysis, EDA, and prototyping, Pandas is still the most productive.
import pandas as pd
import numpy as np
# DataFrame ์์ฑ
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'age': [25, 30, 35, 28],
'city': ['Seoul', 'Busan', 'Seoul', 'Incheon'],
'salary': [50000, 60000, 70000, 55000]
})
# CSV, Excel ์ฝ๊ธฐ/์ฐ๊ธฐ
# df = pd.read_csv('data.csv', encoding='utf-8')
# df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# df.to_csv('output.csv', index=False)
# ๊ธฐ๋ณธ ์ ๋ณด ํ์ธ
print(df.head()) # ์์ 5ํ
print(df.info()) # ์ปฌ๋ผ ํ์
, ๊ฒฐ์ธก์น ์ ๋ณด
print(df.describe()) # ํต๊ณ ์์ฝ
# ์ปฌ๋ผ ์ ํ ๋ฐ ํํฐ๋ง
names = df['name'] # Series ๋ฐํ
subset = df[['name', 'salary']] # DataFrame ๋ฐํ
adults = df[df['age'] >= 30] # ์กฐ๊ฑด ํํฐ๋ง
seoul_high = df[(df['city'] == 'Seoul') & (df['salary'] > 55000)]
# loc (๋ ์ด๋ธ ๊ธฐ๋ฐ) vs iloc (์์น ๊ธฐ๋ฐ)
df.loc[0, 'name'] # 'Alice'
df.iloc[0:2, 1:3] # 0-1ํ, 1-2์ด
df.loc[df['age'] > 25, 'salary'] = df.loc[df['age'] > 25, 'salary'] * 1.1 # ์กฐ๊ฑด๋ถ ์์
# ๊ทธ๋ฃนํ ๋ฐ ์ง๊ณ
city_stats = df.groupby('city').agg({
'salary': ['mean', 'max', 'count'],
'age': 'mean'
}).round(2)
print(city_stats)
# ํผ๋ฒ ํ
์ด๋ธ
# pivot = df.pivot_table(values='salary', index='city', aggfunc='mean')
# DataFrame ๋ณํฉ (SQL JOIN๊ณผ ์ ์ฌ)
orders = pd.DataFrame({
'order_id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Alice'],
'amount': [100, 200, 150]
})
merged = pd.merge(df, orders, on='name', how='left')
# ๊ฒฐ์ธก์น ์ฒ๋ฆฌ
df_with_nan = df.copy()
df_with_nan.loc[0, 'salary'] = np.nan
df_with_nan['salary'].fillna(df_with_nan['salary'].mean(), inplace=True)
# df_with_nan.dropna(subset=['salary']) # ๊ฒฐ์ธก์น ํ ์ญ์
# ์ ์ปฌ๋ผ ์์ฑ (๋ฒกํฐํ ์ฐ์ฐ)
df['salary_category'] = pd.cut(
df['salary'],
bins=[0, 55000, 65000, float('inf')],
labels=['Low', 'Medium', 'High']
)
# apply ํจ์ (์ปค์คํ
๋ณํ)
df['name_upper'] = df['name'].apply(lambda x: x.upper())
# ๋ฉ์๋ ์ฒด์ด๋
result = (df
.query('age >= 25')
.assign(bonus=lambda x: x['salary'] * 0.1)
.groupby('city')['bonus']
.sum()
.sort_values(ascending=False)
)
print(result)
Senior: "Do EDA quickly with Pandas, and switch to Polars if performance issues come up in production."
Junior: "I'm running out of memory reading a 1GB file."
Senior: "Specify dtype to reduce memory, or read it in pieces with the chunksize parameter. Or convert it to Parquet, and the size shrinks dramatically."
Interviewer: "Do you have experience handling large datasets in Pandas?"
Candidate: "I reduced string column memory by 90% with the category type, and read only the necessary columns with read_csv's usecols. For data over 10GB, I used Dask for parallel processing, and finally saved it in Parquet format to shorten I/O time."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "Use vectorized operations instead of iterrows(). It becomes more than 100 times faster."
Developer: "I used a loop because I had to change values conditionally, but I could switch to np.where() or loc."