Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- # Step 1: Read the CSV file
- # Replace 'example.csv' with the path to your CSV file
- df = pd.read_csv('example.csv')
- # Step 2: Access and manipulate the data
- # Access the first 5 rows of the DataFrame
- print("First 5 rows of the DataFrame:")
- print(df.head())
- # Access a specific column by name
- names = df['Name']
- print("\nNames column:")
- print(names)
- # Filter rows based on a condition (e.g., age greater than 30)
- older_than_30 = df[df['Age'] > 30]
- print("\nPeople older than 30:")
- print(older_than_30)
- # Perform various data operations (e.g., calculate mean age)
- mean_age = df['Age'].mean()
- print("\nMean age:", mean_age)
- # Step 3: Save the extracted data to a new CSV file
- # Save the filtered data to a new CSV file
- older_than_30.to_csv('older_than_30.csv', index=False)
- # Save the entire DataFrame to a new CSV file
- df.to_csv('output_data.csv', index=False)
- print("\nData saved to 'older_than_30.csv' and 'output_data.csv'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement