pandas Create a sample DataFrame


Example

import pandas as pd

Create a DataFrame from a dictionary, containing two columns: numbers and colors. Each key represent a column name and the value is a series of data, the content of the column:

df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})

Show contents of dataframe:

print(df)
# Output: 
#   colors  numbers
# 0    red        1
# 1  white        2
# 2   blue        3

Pandas orders columns alphabetically as dict are not ordered. To specify the order, use the columns parameter.

df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']}, 
                  columns=['numbers', 'colors'])

print(df)  
# Output:     
#    numbers colors
# 0        1    red
# 1        2  white
# 2        3   blue