In [1]:
import pandas as pd

In [2]:
pd.__version__


Out[2]:
'1.0.3'

create from lists


In [3]:
names = ['john','mary','peter','gary','anne']
ages = [33,22,45,23,12]

df = pd.DataFrame({
    'names':names,
    'ages':ages
})
df


Out[3]:
names ages
0 john 33
1 mary 22
2 peter 45
3 gary 23
4 anne 12

create from list of dicts


In [4]:
data_dicts = [
    {'name':"john","gender":'male','age':45},
    {'name':"mary", 'gender':"female",'age':19},
    {'name':"peter",'gender':'male', 'age':34}
]

# must reassign since the append method does not work in place
df = pd.DataFrame.from_records(data_dicts)
df


Out[4]:
name gender age
0 john male 45
1 mary female 19
2 peter male 34

create from dict use keys as index


In [ ]:

create an empty dataframe and append rows


In [5]:
df = pd.DataFrame()

# must reassign since the append method does not work in place
df = df.append({'col_a':5,'col_b':10}, ignore_index=True)
df = df.append({'col_a':1,'col_b':100}, ignore_index=True)
df = df.append({'col_a':32,'col_b':999}, ignore_index=True)

df


Out[5]:
col_a col_b
0 5.0 10.0
1 1.0 100.0
2 32.0 999.0

crate dataframe with specific types


In [6]:
data_dicts = [
    {'name':"john","gender":'male','age':45},
    {'name':"mary", 'gender':"female",'age':19},
    {'name':"peter",'gender':'male', 'age':34}
]

# must reassign since the append method does not work in place
df = pd.DataFrame.from_records(data_dicts,)
df


Out[6]:
name gender age
0 john male 45
1 mary female 19
2 peter male 34