Pandas is a widely used Python library for tabular data manipulation and analysis. This guide covers a few everyday DataFrame operations: renaming columns, adding and updating rows, deleting data, sorting, and filtering.
Install Pandas
python -m pip install pandas
Create a DataFrame
import pandas as pd
df = pd.DataFrame(
{
"Name": [
"Braund, Mr. Owen Harris",
"Allen, Mr. William Henry",
"Bonnell, Miss. Elizabeth",
],
"Age": [22, 35, 58],
"Sex": ["male", "male", "female"],
}
)
Rename a Column
df = df.rename(columns={"Sex": "Gender"})
Returning a new DataFrame instead of relying on inplace=True often makes transformation pipelines easier to reason about.
Add a Row
For a small interactive example, assigning through .loc is convenient:
df.loc[len(df)] = ["Smith, Mr. John", 28, "male"]
When adding many rows, build a DataFrame for the new records and combine it once with pd.concat() instead of repeatedly appending one row at a time.
Update Data
Use .loc with a boolean condition:
df.loc[df["Name"] =="Allen, Mr. William Henry", "Age"] =36
For multiple conditions, wrap each expression in parentheses:
result = df[(df["Age"] >=30) & (df["Gender"] =="female")]
Conclusion
Pandas provides concise tools for common data-cleaning and transformation tasks. Once you are comfortable with column selection, .loc, boolean masks, drop(), and sort_values(), you have the foundation for more advanced grouping, joins, aggregation, and time-series analysis.