Created
June 3, 2023 12:34
-
-
Save ozgurozkok/6fbbc4f4551ce1a58909c07ef43f478d to your computer and use it in GitHub Desktop.
Code Samples for Processing CSV Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Read a CSV file | |
import csv | |
with open('data.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row) | |
#Write a CSV file | |
import csv | |
data = [[1, 2, 3], [4, 5, 6]] | |
with open('data.csv', 'w') as f: writer = csv.writer(f) writer.writerows(data) | |
#Manipulate CSV data using pandas | |
import pandas as pd | |
df = pd.read_csv('data.csv') | |
#Print the first 5 rows of the DataFrame | |
print(df.head()) | |
#Print the average value of the 'Age' column | |
print(df['Age'].mean()) | |
#Write the DataFrame to a new CSV file | |
df.to_csv('new_data.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment