Creating DataFrames from scratch
Usually, we create a DataFrame from an existing file or a database, but we can also create one from scratch. We can create a DataFrame from parallel lists of data.
How to do it...
- Create parallel lists with your data in them. Each of these lists will be a column in the DataFrame, so they should have the same type:
>>> import pandas as pd >>> import numpy as np >>> fname = ["Paul", "John", "Richard", "George"] >>> lname = ["McCartney", "Lennon", "Starkey", "Harrison"] >>> birth = [1942, 1940, 1940, 1943]
- Create a dictionary from the lists, mapping the column name to the list:
>>> people = {"first": fname, "last": lname, "birth": birth}
- Create a DataFrame from the dictionary:
>>> beatles = pd.DataFrame(people...