3
Creating and Persisting DataFrames
Introduction
There are many ways to create a DataFrame. This chapter will cover some of the most common ones. It will also show how to persist them.
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 ...
Get Pandas 1.x Cookbook - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.