>> Once we have a CSV file with the data we want to access,
there's a few things you might need to know about
working with CSV files in your Python code,
just aspects that can
mess around with your data if you don't understand them correctly.
So let's talk a bit about how to read and write CSV files,
and use that interacting with
the Pandas DataFrames that Christopher was talking about earlier.
So the read CSV command is what we use to load
a DataFrame from a CSV file.
So let's say we have a nice file here which has names,
city, country, and again that list of airports.
What we can do is we can simply call read_csv,
pass it the name of that file,
put in our nice tidy little data folder that we created,
and it creates a DataFrame for us.
Now something you might notice is that we
did have a row which indicated the name,
city, country, the names of our columns.
For read_csv automatically figured
out that was the values for our column names.
So you'll see the columns have headers of name, city, and country.
You'll also notice that an index was created
automatically because as you saw,
Christopher showed you, DataFrames have an index.
That index was created
automatically for the rows from our CSV file.
Now if you have any problems inside your CSV file,
because sometimes you don't
have control over where these files come from.
One of the things you have to worry about in
your Python code is how are you going to handle
those types of errors in
different situations with your Python code.
So in this case, if you take a look at the CSV file,
there's an extra comma in the row for London.
So we have the airports called Heathrow,
city is London, and then we have these extra two commas.
So in this case,
it actually sees four values instead
of three for that particular row.
By default, that's actually going to
crash and it wont load any data at all into our DataFrame.
But, good news, Pandas has some fantastic features you can use.
For example, there's an error, bad lines.
If you set that to false,
that means just skip the rows of
the errors and load everything you've got.
So in this case, you can see I can load the DataFrame,
and the row for Heathrow London is missing from the loaded data.
Of course, this is only okay if it's all right to skip those rows.
Now what other situations do we deal with?
Well, maybe the data file that you received
doesn't actually have values for the column headers.
So in that case,
it might get confused.
So one of the things we can also do with
Pandas is your Python code you can specify the header is none,
in which case, the read_csv
will automatically create column headers,
it'll just call the columns 0,
1, 2, and so on.
So it will create a column index
to make up for the fact
that it doesn't know what the column names are.
So you need to know when you're writing
Python code if that CSV file has a header row or not,
this is one way to handle it.
But you might have noticed when
Christopher was writing that code to query a DataFrame,
it was really convenient to have names for your columns.
That's okay, we can fix that as well.
So the other thing you can do is you can provide
names for your columns by specifying the names parameter.
So if we add a names parameter here, and we say,
I want to use name, city,
and country as the names with three columns.
There's no column names in my file,
but now you can see I successfully assign
names to each column when it created the DataFrame.
If you have any missing values,
one of the things to get used
to when you're working with Python and
Pandas specifically is this thing called NaN,
and that's the way blank values are going to appear.
So if we take a look at this file,
you can see the record for Schiphol Airport in the Netherlands.
The city value, there's nothing specified.
So in this case, when I call read_csv,
you'll see that it shows
the NaN as the value for the city for Schiphol.
If you have manipulated your data and started working with data,
sometimes you want to save
your changes so that you can go back to them later.
So in addition to being able to read data
from a CSV file into a Pandas DataFrame,
you'll also be able to take data in
a Pandas dataframe and write it to a CSV file.
So we do that with to_csv.
But what you'll see that might throw you off initially,
is the index values will be written to the file as well.
So you can actually see because my DataFrame had an index,
that the file created has these index values saved as well.
That's fine if you want it.
If you don't want it,
then all you have to do is specify index equals false,
in which case it will not include
the index values inside the created CSV file.
Okay. Let's go into some notebooks and try that out in some code.