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()
The value_counts() method counts the number of occurrences of each unique value in a DataFrame column.
# Count the number of occurrences of each unique value in the 'city' column
city_counts = df['city'].value_counts()
Output:
New York 1
Boston 1
Chicago 1
5. replace()
The replace() method replaces values in a DataFrame column with a specified value or method.
# Replace all occurrences of 'New York' with 'NYC' in the 'city' column
df['city'].replace('New York', 'NYC', inplace=True)
Output:
name age city
0 Alice 20.0 NYC
1 Bob 30.0 Boston
2 Charlie 40.0 Chicago
6. astype()
The astype() method converts the data type of a DataFrame column to a specified type.
# Convert the 'age' column to a float data type
df['age'] = df['age'].astype(float)
Output:
name age
0 Alice 20.0
1 Bob 30.0
2 Charlie 40.0
7. to_csv()
The to_csv() method writes a DataFrame to a CSV file.
# Write the DataFrame to a CSV file
df.to_csv('output.csv', index=False)
8. to_excel()
The to_excel() method writes a DataFrame to an Excel file.
# Write the DataFrame to an Excel file
df.to_excel('output.xlsx', index=False)
9. read_csv()
The read_csv() method reads a CSV file into a DataFrame.
# Read a CSV file into a DataFrame
df = pd.read_csv('input.csv')
10. read_excel()
The read_excel() method reads an Excel file into a DataFrame.
# Read an Excel file into a DataFrame
df = pd.read_excel('input.xlsx')
Conclusion
These are just 10 of the most commonly used Pandas DataFrame methods. By understanding how to use these methods, you can easily perform a variety of data manipulation and analysis tasks in Python.
Comments
Post a Comment
Oof!