Dealing with Rows and Columns in Pandas DataFrame
0 921
Mastering Rows and Columns in Pandas DataFrame
In data analysis, efficiently manipulating rows and columns is crucial for effective data exploration and cleaning. Pandas, a powerful Python library, provides intuitive methods to handle these operations seamlessly. Let's delve into how to manage rows and columns in a Pandas DataFrame.
Selecting Columns
Accessing specific columns in a DataFrame can be achieved by referencing the column name:
import pandas as pd
# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
# Select 'Name' and 'City' columns
print(df[['Name', 'City']])
This code snippet selects the 'Name' and 'City' columns from the DataFrame and displays them.
Adding Columns
To introduce a new column to the DataFrame, you can assign a list or a Series to a new column name:
# Add 'Country' column
df['Country'] = ['USA', 'USA', 'USA']
print(df)
This adds a 'Country' column with specified values to the DataFrame.
Renaming Columns
Renaming columns is straightforward using the rename() method:
# Rename 'City' to 'Location'
df.rename(columns={'City': 'Location'}, inplace=True)
print(df)
This renames the 'City' column to 'Location' in the DataFrame.
Deleting Columns
To remove columns, utilize the drop() method:
# Drop 'Country' column
df.drop('Country', axis=1, inplace=True)
print(df)
This deletes the 'Country' column from the DataFrame.
Selecting Rows
Rows can be selected using the loc[] or iloc[] accessors:
# Select rows by label
print(df.loc[0])
# Select rows by index position
print(df.iloc[1])
The loc[] accessor selects rows based on labels, while iloc[] selects rows by index position.
Adding Rows
To append a new row, use the loc[] accessor with a new index label:
# Add new row
df.loc[3] = ['David', 40, 'San Francisco']
print(df)
This adds a new row with the specified data at index 3.
Renaming Rows
Renaming rows involves modifying the index labels:
# Rename index 0 to 'A'
df.rename(index={0: 'A'}, inplace=True)
print(df)
This changes the index label from 0 to 'A'.
Deleting Rows
To remove rows, use the drop() method:
# Drop row with index 'A'
df.drop('A', axis=0, inplace=True)
print(df)
This deletes the row with index label 'A' from the DataFrame.
Conclusion
Understanding how to manipulate rows and columns in a Pandas DataFrame is fundamental for data analysis tasks. By mastering these operations, you can efficiently clean, transform, and analyze your data, paving the way for more complex data science workflows.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments