How to modify Pandas Dataframe?
Pandas is a powerful and widely-used library in Python for data manipulation and analysis. One of its core data structures is the DataFrame, a two-dimensional table of data with rows and columns. In this article, we will explore some ways to modify DataFrames in Pandas.
Preparing a dataframe to work on
import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'Chicago', 'Los Angeles']} df = pd.DataFrame(data) df.head()
This will create a DataFrame with three rows and three columns, named ‘name’, ‘age’, and ‘city’.
Add new column
One common way to modify a DataFrame is to add or remove columns. To add a new column, we can simply assign a new value to a column that does not yet exist:
df['gender'] = ['F', 'M', 'M'] df.head()
Remove a column
To remove a column, we can use the drop()
method and specify the column name and the axis as 1:
df = df.drop('gender', axis=1) df.head()
Read more in the official documentation
Rename columns
To rename columns in a Pandas dataframe, you can use the rename()
function. The basic syntax is:
dataframe.rename(columns={'old_name_1': 'new_name_1', 'old_name_2': 'new_name_2'}, inplace=True)
The rename()
function takes a dictionary as an argument, where the keys are the old column names and the values are the new column names. The inplace parameter tells the function to make the changes to the original dataframe, instead of returning a new dataframe with the changes.
Read more in the official documentation
Data types
To set data types within a Pandas dataframe, you can use the astype()
method. The basic syntax is:
dataframe = dataframe.astype({'column_name_1': data_type_1, 'column_name_2': data_type_2})
Where data_type_1
and data_type_2
are the desired data types for the columns column_name_1
and column_name_2
, respectively. For example, you can use int
, float
, str
, bool
, datetime64
, etc.
You can also use the dtype
attribute to set the data type for a specific column:
dataframe['column_name'] = dataframe['column_name'].astype(data_type)
Read more in the official documentation
Header image is by Pascal Müller on Unsplash
editor's pick
latest video
news via inbox
Nulla turp dis cursus. Integer liberos euismod pretium faucibua