Creating pandas DataFrames

Python for MATLAB Users

Justin Kiggins

Product Manager

Date, AveragePrice, Total Volume, 4046, 4225, 4770
2015-12-27, 1.33, 64236.62, 1036.74, 54454.85, 48.16
2015-12-20, 1.35, 54876.98, 674.28, 44638.81, 58.33
2015-12-13, 0.93, 118220.22, 794.70, 109149.67,130.50
2015-12-06, 1.08, 78992.15, 1132.00, 71976.41, 72.58
2015-11-29, 1.28, 51039.60, 941.48, 43838.39, 75.78
import pandas as pd
avocados = pd.read_csv('avocados.csv')

avocados.head()
         Date  AveragePrice  Total Volume     4046       4225    4770
0  2015-12-27          1.33      64236.62  1036.74   54454.85   48.16
1  2015-12-20          1.35      54876.98   674.28   44638.81   58.33
2  2015-12-13          0.93     118220.22   794.70  109149.67  130.50
3  2015-12-06          1.08      78992.15  1132.00   71976.41   72.58
4  2015-11-29          1.28      51039.60   941.48   43838.39   75.78
avocados = pd.read_csv('avocados.csv', index_col=0)
Python for MATLAB Users

From a dictionary of lists

import pandas as pd
pd.DataFrame()

forecast_raw = { 'weekday': ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'], 'rain': [True, False, False, False, True, True, False], 'temp': [68, 72, 73, 75, 67, 68, 68] }
forecast = pd.DataFrame(forecast_raw) print(forecast)
    rain  temp weekday
0   True    68     Mon
1  False    72    Tues
2  False    73     Wed
3  False    75   Thurs
4   True    67     Fri
5   True    68     Sat
6  False    68     Sun
Python for MATLAB Users

From a list of dictionaries

forecast_raw = [ {'rain': True, 'temp': 68, 'weekday': 'Mon'},
                 {'rain': False, 'temp': 72, 'weekday': 'Tues'},
                 {'rain': False, 'temp': 73, 'weekday': 'Wed'},
                 {'rain': False, 'temp': 75, 'weekday': 'Thurs'},
                 {'rain': True, 'temp': 67, 'weekday': 'Fri'},
                 {'rain': True, 'temp': 68, 'weekday': 'Sat'},
                 {'rain': False, 'temp': 68, 'weekday': 'Sun'}, ]

import pandas as pd forecast = pd.DataFrame(forecast_raw) print(forecast)
    rain  temp weekday
0   True    68     Mon
1  False    72    Tues
2  False    73     Wed
3  False    75   Thurs
4   True    67     Fri
5   True    68     Sat
6  False    68     Sun
Python for MATLAB Users

Let's practice

Python for MATLAB Users

Preparing Video For Download...