|
| 1 | +# ------------------------------------------------------------------------------------ |
| 2 | +# Tutorial: creating a dataframe from lists |
| 3 | +# ------------------------------------------------------------------------------------ |
| 4 | +# Think of a dataframe as an excel spreadsheet. All it is is a two-dimensional structure consisting of rows and columns. |
| 5 | +# Here I'll show you how to create a datframe with three columns using a list of lists. |
| 6 | +# Remember a list is created using square brackets []. So list of lists is different lists all wraped in a square bracket i.e. [['Sophie'], ['Jade']] |
| 7 | +# PS: anywhere you see a step, remove the hashtag before the code and remove everything in the bracket (). This does not include step 2! |
| 8 | +# e.g. in your practice, copy only "import pandas as pd" not "# import pandas as pd (step 2)" |
| 9 | + |
| 10 | +# Firstly, pip install pandas then import pandas as pd |
| 11 | +# pip install pandas (step 1) |
| 12 | +# import pandas as pd (step 2) |
| 13 | +# Creating a list of lists with two strings and an integer each |
| 14 | +list = [['Purple Hibiscus', 'Chimamanda', 2001], ['Things Fall Apart', 'Chinua', 1980]] # Step 3 |
| 15 | + |
| 16 | +# Storing the transformation from list to dataframe in a variable |
| 17 | +# df means dataframe but you can call your variable anything |
| 18 | +# df = pd.DataFrame(list, columns=['BookTitle', 'AuthorFirstName', 'YearPublished']) (Step 4) |
| 19 | +# df (step 5) |
| 20 | + |
| 21 | +# There are different ways to create a dataframe from lists. This was just one of them! |
| 22 | + |
| 23 | +# ------------------------------------------------------------------------------------ |
| 24 | +# Challenge: get the right answer ! |
| 25 | +# 1. Make your own list of lists |
| 26 | +# 2. Store it in another variable that is not called df |
| 27 | +# 3. Create a dataframe from it |
| 28 | +# ------------------------------------------------------------------------------------ |
0 commit comments