Pandas > Create data objects

Name
Code
Output
# creating dataframe from dictionary allows to specify different data source/type for each column

dataframe = pandas.DataFrame({
    'C1': pandas.date_range('20170101', periods=4),
    'C2' : [10,20,30,40],
    'C3': pandas.Categorical(['A','B','C','D']),
    'C4': 1})
print ("dataframe")
print (dataframe)
dataframe
          C1  C2 C3  C4
0 2017-01-01  10  A   1
1 2017-01-02  20  B   1
2 2017-01-03  30  C   1
3 2017-01-04  40  D   1
array = numpy.array([(1,2,3), (4,5,6),(7,8,9)])
dataframe = pandas.DataFrame(array,columns=['C1','C2','C3'])
print ("dataframe")
print (dataframe)
dataframe
   C1  C2  C3
0   1   2   3
1   4   5   6
2   7   8   9
dataframe = pandas.DataFrame(numpy.random.randint(0,100,size=(5, 4)), columns=['C1','C2','C3','C4'])
print ("dataframe")
print (dataframe)
dataframe
   C1  C2  C3  C4
0  27  32  73  54
1  63  21  46  34
2  68  89  31  36
3  16  47  49  64
4  28  20  82  84
# muti index contains multiple numpy arrays

multi_index = [numpy.array(['Alpha','Beta','Gamma','Alpha','Beta']), numpy.array([1,2,3,4,5])]
dataframe = pandas.DataFrame(numpy.random.randint(0,10,size=(5, 3)), columns=['C1','C2','C3'],index = multi_index)
print ("dataframe")
print (dataframe)
print ("rows Alpha")
print (dataframe.loc['Alpha'])
print ("row Alpha 4")
print (dataframe.loc['Alpha',4])
dataframe
         C1  C2  C3
Alpha 1   7   7   6
Beta  2   9   3   1
Gamma 3   7   4   8
Alpha 4   7   3   2
Beta  5   4   1   3
rows Alpha
   C1  C2  C3
1   7   7   6
4   7   3   2
row Alpha 4
C1    7
C2    3
C3    2
Name: (Alpha, 4), dtype: int64
# series is one dimentional array with axis

series = pandas.Series([1,5,10,15,20])
print ("series")
print (series)
series
0     1
1     5
2    10
3    15
4    20
dtype: int64
data = {'a' : 0., 'b' : 1., 'c' : 2.}
series = pandas.Series({'a' : 1, 'b' : 2, 'c' : 3})
print ("series")
print (series)
series
a    1
b    2
c    3
dtype: int64
months_index = pandas.date_range('1/1/2015', periods=12, freq='M')
series = pandas.Series(numpy.random.randint(0,100,size=(12,)),index = months_index)
print ("series")
print (series)
series
2015-01-31    45
2015-02-28    59
2015-03-31    66
2015-04-30    50
2015-05-31    82
2015-06-30     2
2015-07-31    18
2015-08-31    41
2015-09-30    63
2015-10-31    37
2015-11-30    89
2015-12-31    31
Freq: M, dtype: int64