Skip to main content

Posts

Showing posts with the label read_csv()

Writing and Reading CSV Files with Python Pandas

Pandas, a powerful Python library for data manipulation and analysis, provides a comprehensive set of methods for reading and writing CSV (Comma-Separated Values) files. These methods are designed to be efficient, flexible, and easy to use. Writing Data to CSV Files To write data to a CSV file using Pandas, you can use the to_csv() method attached to the DataFrame or Series object. This method takes the filename as its first argument and supports various options to control the formatting and behavior of the output CSV file. import pandas as pd # Create a DataFrame df = pd.DataFrame({'Name': ['John', 'Mary', 'Bob'], 'Age': [25, 30, 35]}) # Write the DataFrame to a CSV file df.to_csv('data.csv', index=False) Reading Data from CSV Files To read data from a CSV file into a Pandas DataFrame, you can use the read_csv() function. This function takes the filename as its first argument and also supports various options to control the parsing an...

10 Commonly Used Pandas DataFrame Methods

In this blog post, we will discuss 10 commonly used Pandas DataFrame methods, along with examples of how to use them. 1. fillna() The fillna() method fills missing values in a DataFrame with a specified value or method. # Fill missing values in the 'age' column with the mean age df['age'].fillna(df['age'].mean(), inplace=True) Output: name age 0 Alice 20.0 1 Bob 30.0 2 Charlie 40.0 2. dropna() The dropna() method removes rows or columns with missing values from a DataFrame. # Remove rows with missing values in the 'name' column df.dropna(subset=['name'], inplace=True) Output: name age 0 Alice 20.0 1 Bob 30.0 2 Charlie 40.0 3. unique() The unique() method returns the unique values in a DataFrame column. # Get the unique values in the 'city' column unique_cities = df['city'].unique() print(unique_cities) Output: ['New York', 'Boston', 'Chicago'] 4. value_counts()...

Topics

Show more