Pandas Dataframe All Operations 1735471870
Pandas Dataframe All Operations 1735471870
Creating a DataFrame #
import pandas as pd
df = pd.DataFrame(data)
print(df)
Basic Operations #
1. Viewing Data #
2. Accessing Data #
Data Manipulation #
1. Adding Columns #
# Dropping a row
df = df.drop(1, axis=0)
3. Renaming Columns #
4. Filtering Data #
5. Sorting Data #
# Sort by Age
sorted_df = df.sort_values(by='Age', ascending=True)
print(sorted_df)
1. Aggregation Functions #
print(df['Salary'].sum())
print(df['Age'].mean())
2. Grouping Data #
grouped = df.groupby('Department').mean()
print(grouped)
print(df.isnull())
1. Merging #
data2 = {
'Name': ['Alice', 'Bob'],
'City': ['New York', 'Los Angeles']
}
df2 = pd.DataFrame(data2)
merged_df = pd.merge(df, df2, on='Name')
print(merged_df)
2. Concatenation #
3. Joining #
joined_df = df.set_index('Name').join(df2.set_index('Name'))
print(joined_df)
Advanced Operations #
1. Applying Functions #
2. Pivot Tables #
1. Saving to a File #
# Save to CSV
df.to_csv('data.csv', index=False)
# Save to Excel
df.to_excel('data.xlsx', index=False)
codeInSpark.com
# Load from CSV
new_df = pd.read_csv('data.csv')
Conclusion #
Pandas DataFrame provides a versatile and efficient way to handle and analyze structured data. Mastering
these operations will significantly enhance your data analysis workflow.
codeInSpark.com