Pandas Lesson 6 Create a DataFrame

  1. Create an Empty DataFrame
  2. Create a DataFrame from a List
  3. Create a DataFrame from a Dictionary

1) Create an Empty DataFrame


import pandas as pd

data = pd.DataFrame()
print(data)
Notebook Output

2) Create a DataFrame from a List

If you have data that would fit into rows of a Dataframe then use these methods.


weather = [["Houston",99,90,100],["Dallas",110,90,99],["El Paso",99,100,90]]
labels = ["City","Monday","Tuesday","Wednesday"]

data = pd.DataFrame(weather, columns = labels)
data
Notebook Output

weather = [["Houston",99,90,100],["Dallas",110,90,99],["El Paso",99,100,90]]
labels = ["City","Monday","Tuesday","Wednesday"]


data = pd.DataFrame.from_records(weather,columns = labels)
data
Notebook Output

3) Create a DataFrame from a Dictionary

If you have data that would fit into columns in a DataFrame then use this method.


weather =  {"City":["Houston","Dallas","El Paso"],
            "Monday":[99,110,99],
            "Tuesday":[90,90,100],
            "Wednesday":[100,99,90]}

data = pd.DataFrame.from_dict(weather)
data
Notebook Output

If you have data that would fit into rows use this method.


import pandas as pd

weather = [{"City":"Houston","Monday":99,"Tuesday":90,"Wednesday":100},
           {"City":"Dallas","Monday":110,"Tuesday":90,"Wednesday":99},
           {"City":"El Paso","Monday":99,"Tuesday":100,"Wednesday":90}]
data = pd.DataFrame(weather)
data
Notebook Output