Loading Data into a DataFrame
Lesson 3 of 19 in Coddy's Pandas Analytics course.
Usually, when working with data, it is stored in a CSV (Comma Separated Values) file. Every line in this file represents a line, and the first row represents the column names. For example:
col1,col2,col3
val1,val2,val3
val4,val5,val6
val7,val8,val9Here we have 3 columns: col1, col2, col3, and 3 rows.
To load a CSV file into a dataframe:
import pandas as pd
df = pd.read_csv('filename.csv')Challenge
EasyCreate a function named csv_convertor that accepts a file name, loads it into a dataframe, and finally prints the dataframe.
If the CSV file is empty, print "Empty file".
Try it yourself
import pandas as pd
def csv_convertor(filename):
# Write code hereAll lessons in Pandas Analytics
4Data Analysis with Pandas
Descriptive StatisticsGrouping and Aggregating DataDifferent AggregationsMerge & Concat2Working with the DataFrame
Understanding DataFramesAccessing DataData Cleaning - Missing dataData Cleaning - More tools3Data Manipulation with Pandas
Return Requested ResultFilter DataAdd & DeleteModify DataModify StringsCustom Modifications