Give Me More Business for My Cart




Pay Notebook Creator: Stelios Phanartzis0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [14]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [15]:
s = pd.Series([1,3,5,np.nan,6,8])
In [16]:
s
Out[16]:
0    1.0
1    3.0
2    5.0
3    NaN
4    6.0
5    8.0
dtype: float64
In [17]:
dates = pd.date_range('20130101', periods=6)
In [18]:
dates
Out[18]:
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')
In [19]:
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
In [20]:
df
Out[20]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
2013-01-01 0.912308 1.042205 0.116620 -0.249330
2013-01-02 0.050897 -0.857851 1.892152 0.089577
2013-01-03 -0.276137 0.870425 -0.292043 1.759634
2013-01-04 0.810178 -0.394288 -0.298116 -0.504609
2013-01-05 1.347367 -0.987219 0.317209 -1.278711
2013-01-06 -0.888704 0.832839 0.526465 1.173505
In [21]:
df2 = pd.DataFrame({'A' : 1.,
                    'B' : pd.Timestamp('20130102'),
                    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                    'D' : np.array([3] * 4,dtype='int32'),
                    'E' : pd.Categorical(["test","train","test","train"]),
                    'F' : 'foo'})
In [22]:
df2
Out[22]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
In [23]:
df2.dtypes
Out[23]:
A           float64
B    datetime64[ns]
C           float32
D             int32
E          category
F            object
dtype: object
In [26]:
df.head()
Out[26]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
2013-01-01 0.912308 1.042205 0.116620 -0.249330
2013-01-02 0.050897 -0.857851 1.892152 0.089577
2013-01-03 -0.276137 0.870425 -0.292043 1.759634
2013-01-04 0.810178 -0.394288 -0.298116 -0.504609
2013-01-05 1.347367 -0.987219 0.317209 -1.278711
In [27]:
df.tail()
Out[27]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
2013-01-02 0.050897 -0.857851 1.892152 0.089577
2013-01-03 -0.276137 0.870425 -0.292043 1.759634
2013-01-04 0.810178 -0.394288 -0.298116 -0.504609
2013-01-05 1.347367 -0.987219 0.317209 -1.278711
2013-01-06 -0.888704 0.832839 0.526465 1.173505
In [28]:
df.index
Out[28]:
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')
In [29]:
df.columns
Out[29]:
Index(['A', 'B', 'C', 'D'], dtype='object')
In [30]:
df.values
Out[30]:
array([[ 0.91230835,  1.04220519,  0.11662036, -0.24932956],
       [ 0.05089737, -0.85785106,  1.89215172,  0.0895768 ],
       [-0.27613658,  0.87042539, -0.29204261,  1.75963441],
       [ 0.81017848, -0.3942882 , -0.29811603, -0.5046093 ],
       [ 1.34736699, -0.98721882,  0.31720941, -1.27871095],
       [-0.88870378,  0.83283915,  0.52646464,  1.17350527]])
In [31]:
df.describe()
Out[31]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
count 6.000000 6.000000 6.000000 6.000000
mean 0.325985 0.084352 0.377048 0.165011
std 0.840849 0.933888 0.811517 1.119819
min -0.888704 -0.987219 -0.298116 -1.278711
25% -0.194378 -0.741960 -0.189877 -0.440789
50% 0.430538 0.219275 0.216915 -0.079876
75% 0.886776 0.861029 0.474151 0.902523
max 1.347367 1.042205 1.892152 1.759634
In [33]:
df.T
Out[33]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
2013-01-01 00:00:00 2013-01-02 00:00:00 2013-01-03 00:00:00 2013-01-04 00:00:00 2013-01-05 00:00:00 2013-01-06 00:00:00
A 0.912308 0.050897 -0.276137 0.810178 1.347367 -0.888704
B 1.042205 -0.857851 0.870425 -0.394288 -0.987219 0.832839
C 0.116620 1.892152 -0.292043 -0.298116 0.317209 0.526465
D -0.249330 0.089577 1.759634 -0.504609 -1.278711 1.173505
In [35]:
df.sort_index(axis=1, ascending=False)
Out[35]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
D C B A
2013-01-01 -0.249330 0.116620 1.042205 0.912308
2013-01-02 0.089577 1.892152 -0.857851 0.050897
2013-01-03 1.759634 -0.292043 0.870425 -0.276137
2013-01-04 -0.504609 -0.298116 -0.394288 0.810178
2013-01-05 -1.278711 0.317209 -0.987219 1.347367
2013-01-06 1.173505 0.526465 0.832839 -0.888704
In [36]:
df.sort_values(by='B')
Out[36]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
2013-01-05 1.347367 -0.987219 0.317209 -1.278711
2013-01-02 0.050897 -0.857851 1.892152 0.089577
2013-01-04 0.810178 -0.394288 -0.298116 -0.504609
2013-01-06 -0.888704 0.832839 0.526465 1.173505
2013-01-03 -0.276137 0.870425 -0.292043 1.759634
2013-01-01 0.912308 1.042205 0.116620 -0.249330
In [40]:
df['A']
Out[40]:
2013-01-01    0.912308
2013-01-02    0.050897
2013-01-03   -0.276137
2013-01-04    0.810178
2013-01-05    1.347367
2013-01-06   -0.888704
Freq: D, Name: A, dtype: float64
In [41]:
df[0:3]
Out[41]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
A B C D
2013-01-01 0.912308 1.042205 0.116620 -0.249330
2013-01-02 0.050897 -0.857851 1.892152 0.089577
2013-01-03 -0.276137 0.870425 -0.292043 1.759634
In [ ]:
df