Advertisement
1fractal

Untitled

Sep 28th, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. # Step 1: Read the CSV file
  4. # Replace 'example.csv' with the path to your CSV file
  5. df = pd.read_csv('example.csv')
  6.  
  7. # Step 2: Access and manipulate the data
  8. # Access the first 5 rows of the DataFrame
  9. print("First 5 rows of the DataFrame:")
  10. print(df.head())
  11.  
  12. # Access a specific column by name
  13. names = df['Name']
  14. print("\nNames column:")
  15. print(names)
  16.  
  17. # Filter rows based on a condition (e.g., age greater than 30)
  18. older_than_30 = df[df['Age'] > 30]
  19. print("\nPeople older than 30:")
  20. print(older_than_30)
  21.  
  22. # Perform various data operations (e.g., calculate mean age)
  23. mean_age = df['Age'].mean()
  24. print("\nMean age:", mean_age)
  25.  
  26. # Step 3: Save the extracted data to a new CSV file
  27. # Save the filtered data to a new CSV file
  28. older_than_30.to_csv('older_than_30.csv', index=False)
  29.  
  30. # Save the entire DataFrame to a new CSV file
  31. df.to_csv('output_data.csv', index=False)
  32.  
  33. print("\nData saved to 'older_than_30.csv' and 'output_data.csv'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement