How to manipulate CSV data using pandas?

Published on Aug. 22, 2023, 12:17 p.m.

To manipulate CSV data using Pandas, you can first import the Pandas library and use the read_csv() function to read the CSV file into a Pandas DataFrame object. Here’s an example code snippet that reads a CSV file named ‘example.csv’, modifies the data, and saves the changes back to a CSV file:

import pandas as pd

# read the CSV file into a DataFrame
df = pd.read_csv('example.csv')

# modify the data in the DataFrame as needed
df['entry'] = df['entry'].astype(str)  # convert 'entry' column to string data type

# write the modified data to a CSV file
df.to_csv('modified_example.csv', index=False)  # index=False prevents index column being added to CSV

In the above example, we first use pd.read_csv() to read ‘example.csv’ file into a Pandas DataFrame object named ‘df’. We then modify the data in the DataFrame object by converting the ‘entry’ column to string data type using .astype() method. Finally, we use .to_csv() method to save the DataFrame object as a CSV file named ‘modified_example.csv’, with index=False to prevent the index column being added to the output.

Note that Pandas provides many other methods for manipulating CSV data, such as sorting and filtering data, aggregating data, and merging multiple CSV files. You can refer to the Pandas documentation for more details.

Tags: