A CSV (Comma Separated Values) file is a file with values seperated by a comma. Its often used to import and export with databases and spreadsheets.
Values are mostly seperated by comma. Sometimes another character is used like a semicolon, the seperation character is called a delimiter.
Data Analysis with Pandas and Python
Pandas is a data analysis library. If you work with data a lot, using pandas is way better.
Lets say you have a csv file containing nation statistics:
We can read a csv with the lines:
Pandas works with dataframes which hold all data. Thats why we can use the rows like df[‘name’].
If you don’t want to use pandas, you can use the csv module to read csv files.
This is as simple as:
We can access individual cells like so:
To get started, you will need the Python interpreter or a Python IDE. An IDE is a tool that will make the experience of software development much better.
Python programs are simply a collection of text files. If you want something more sophisticated than notepad for editing, you will need a Python IDEs (recommend). A Python IDE will make programming Python easier.
You can download Python interpreter here: https://www.python.org/downloads/
The Python interpreter is a command line program, we’ll discuss it in the next lecture.
An IDE generally supports listing all program files, syntax highlighting and other features. There are lots of Python IDEs you could choose from.
Using one of these Python IDEs makes programming easier than in say, notepad. It will automatically color the text like the example below:
Learn how to run Python code?
You can execute Python code from the terminal or from a Python IDE. An IDE is a graphical environment that assitsts in software development.
If you are new to Python, I recommend this course:
All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py.
A program made in Python can be one or more .py files.
To run a Python program, you need to have Python installed. Start the terminal.
In the terminal, type the command below to test if Python is installed
Open a terminal and enter the directory of your program. The directory may contain more than one python (.py) file.
In the terminal type:
where file.py is the name of your program. If you have more than one file, the main program is often the name of the program itself;
Sometimes an easier way to execute Python programs is using a Python IDE. What is an IDE?
An IDE is a tool that assists in software development. Its a software program that has many features like code coloring (syntax highlighting), file navigation, quick search and much more.
In most IDEs, there’s a play button that instantly runs the Python program. In other words, in a Python IDE, you simply press the ‘play’ button.
The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.
Want to get keyboard input?
To get keyboard input, use the input function. Inside the terminal you’ll be able to type text.
In Python 3 you can use the input() function, older versions of Python have the raw_input() function.
The input function has a return variable. In this case the keyboard input. If we do not store the return variable into a programs variable, we lose it. That’s why we write a variable to store the result in.
To get a text value:
name = input("Enter a name: ")
This will show you:
You can now give keyboard input, it will be stored in the variable name.
To get a whole number:
x = int(input("What is x? "))
To get a decimal number:
x = float(input("Write a number"))
Text output is one of the basics in Python programming. Not all Programs have graphical user interfaces, text screens often suffice.
You can output to the terminal with print function. This function displays text on your screen, it won’t print.
The terminal is a very simple interface for Python programs. While not as shiny as a GUI or web app, it’s good enough to cover the basics in.
Create a new program (text file) in your IDE or code editor.
Name the file hello.py. It only needs one line of code.
To output text to the screen you will need this line::
Run the program (from terminal: python hello.py)
If you run the program:
The program above prints everything on a single line. At some point you’ll want to write multiple lines.
To write multiple lines, add the ‘\n’ character:
print("Hello World\nThis is a message")
\ncreate a new line
To print variables:
x = 3
This will show:
To print multiple variables on one line:
x = 2
Will give you:
x = 2, y = 3