| name | csv |
| description | Use this skill any time a CSV (Comma-Separated Values) file is the primary input or output. This includes tasks to: read, parse, analyze, clean, transform, or export CSV files; create new CSV files from data; merge or split CSV files; perform data analysis on tabular data in CSV format. If the user mentions a CSV file by name, extension, or path, use this skill. |
CSV File Handling in EverFern
Overview
Users may ask you to read, analyze, create, or modify CSV files. Use Python with pandas for robust CSV operations on Windows with absolute paths (e.g., C:\Users\Username\Downloads\data.csv).
Reading and Analyzing CSV Files
Basic CSV Operations with pandas
import pandas as pd
df = pd.read_csv(r'C:\path\to\file.csv')
print(df.head())
print(df.info())
print(df.describe())
print(df.shape)
print(df.columns.tolist())
print(df['ColumnName'].value_counts())
print(df.isnull().sum())
Data Cleaning and Transformation
Remove Duplicates
df_clean = df.drop_duplicates()
df_clean.to_csv(r'C:\path\to\output.csv', index=False)
Filter Rows
filtered = df[df['ColumnName'] > 100]
filtered.to_csv(r'C:\path\to\filtered.csv', index=False)
Select Specific Columns
selected = df[['Column1', 'Column2', 'Column3']]
selected.to_csv(r'C:\path\to\selected.csv', index=False)
Handle Missing Values
df_clean = df.dropna()
df_filled = df.fillna(0)
df_filled = df.fillna(df.mean())
df_filled.to_csv(r'C:\path\to\output.csv', index=False)
Creating CSV Files
From Python Data
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['NYC', 'LA', 'Chicago']
}
df = pd.DataFrame(data)
df.to_csv(r'C:\path\to\new_file.csv', index=False)
From Lists
import csv
data = [
['Name', 'Email', 'Phone'],
['John', 'john@example.com', '123-456-7890'],
['Jane', 'jane@example.com', '098-765-4321']
]
with open(r'C:\path\to\file.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)
Data Analysis
Group By and Aggregation
summary = df.groupby('Category')['Amount'].sum()
summary.to_csv(r'C:\path\to\summary.csv')
summary = df.groupby('Category').agg({
'Amount': 'sum',
'Count': 'count',
'Average': 'mean'
})
summary.to_csv(r'C:\path\to\summary.csv')
Merge CSV Files
df1 = pd.read_csv(r'C:\path\to\file1.csv')
df2 = pd.read_csv(r'C:\path\to\file2.csv')
merged = pd.concat([df1, df2], ignore_index=True)
merged = pd.merge(df1, df2, on='CommonColumn')
merged.to_csv(r'C:\path\to\merged.csv', index=False)
Export Options
df.to_csv(r'C:\path\to\file.tsv', sep='\t', index=False)
df.to_csv(r'C:\path\to\file.csv', index=False, encoding='utf-8')
df.to_csv(r'C:\path\to\file.csv', sep=';', index=False)
Tips and Best Practices
- Always check data types: Use
df.dtypes to verify columns were parsed correctly
- Specify encoding: Use
encoding='utf-8' or encoding='latin-1' if needed
- Index handling: Use
index=False when writing to avoid extra row numbers
- Large files: Use
chunksize parameter for memory efficiency: pd.read_csv(file, chunksize=1000)
- Dialects: Specify dialect for special CSV formats:
pd.read_csv(file, dialect='excel-tab')