2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -6,7 +6,7 @@
In [1]:
```
```py
%matplotlib inline
import pandas as pd
@@ -32,14 +32,14 @@ import matplotlib.pyplot as plt
In [2]:
```
```py
s = pd.Series([1,3,5,np.nan,6,8])
print s
```
```
```py
0 1
1 3
2 5
@@ -58,14 +58,14 @@ dtype: float64
In [3]:
```
```py
dates = pd.date_range('20130101', periods=6)
print dates
```
```
```py
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06'],
dtype='datetime64[ns]', freq='D')
@@ -76,7 +76,7 @@ DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
In [4]:
```
```py
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
df
@@ -100,7 +100,7 @@ Out[4]:
In [5]:
```
```py
df2 = pd.DataFrame({'A' : 1.,
'B' : pd.Timestamp('20130102'),
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
@@ -127,14 +127,14 @@ Out[5]:
In [6]:
```
```py
df2.dtypes
```
Out[6]:
```
```py
A float64
B datetime64[ns]
C float32
@@ -152,7 +152,7 @@ dtype: object
In [7]:
```
```py
df.head()
```
@@ -171,7 +171,7 @@ Out[7]:
In [8]:
```
```py
df.tail(3)
```
@@ -190,14 +190,14 @@ Out[8]:
In [9]:
```
```py
df.index
```
Out[9]:
```
```py
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06'],
dtype='datetime64[ns]', freq='D')
@@ -207,14 +207,14 @@ DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
In [10]:
```
```py
df.columns
```
Out[10]:
```
```py
Index([u'A', u'B', u'C', u'D'], dtype='object')
```
@@ -222,14 +222,14 @@ Index([u'A', u'B', u'C', u'D'], dtype='object')
In [11]:
```
```py
df.values
```
Out[11]:
```
```py
array([[-0.60593585, -0.86165752, -1.00192387, 1.52858443],
[-0.16540784, 0.38833783, 1.18718697, 1.81981793],
[ 0.06525454, -1.60807414, -1.2823306 , -0.28606716],
@@ -244,7 +244,7 @@ array([[-0.60593585, -0.86165752, -1.00192387, 1.52858443],
In [12]:
```
```py
df.describe()
```
@@ -266,7 +266,7 @@ Out[12]:
In [13]:
```
```py
df.T
```
@@ -286,7 +286,7 @@ Out[13]:
In [14]:
```
```py
df.sort_index(ascending=False)
```
@@ -304,7 +304,7 @@ Out[14]:
In [15]:
```
```py
df.sort_index(axis=1, ascending=False)
```
@@ -324,7 +324,7 @@ Out[15]:
In [16]:
```
```py
df.sort_values(by="B")
```
@@ -350,14 +350,14 @@ Out[16]:
In [17]:
```
```py
df["A"]
```
Out[17]:
```
```py
2013-01-01 -0.605936
2013-01-02 -0.165408
2013-01-03 0.065255
@@ -371,14 +371,14 @@ Freq: D, Name: A, dtype: float64
In [18]:
```
```py
df.A
```
Out[18]:
```
```py
2013-01-01 -0.605936
2013-01-02 -0.165408
2013-01-03 0.065255
@@ -392,7 +392,7 @@ Freq: D, Name: A, dtype: float64
In [19]:
```
```py
df[0:3]
```
@@ -409,7 +409,7 @@ Out[19]:
In [20]:
```
```py
df["20130101":"20130103"]
```
@@ -428,14 +428,14 @@ Out[20]:
In [21]:
```
```py
df.loc[dates[0]]
```
Out[21]:
```
```py
A -0.605936
B -0.861658
C -1.001924
@@ -447,7 +447,7 @@ Name: 2013-01-01 00:00:00, dtype: float64
In [22]:
```
```py
df.loc[:,['A','B']]
```
@@ -467,7 +467,7 @@ Out[22]:
In [23]:
```
```py
df.loc['20130102':'20130104',['A','B']]
```
@@ -484,14 +484,14 @@ Out[23]:
In [24]:
```
```py
df.loc['20130102',['A','B']]
```
Out[24]:
```
```py
A -0.165408
B 0.388338
Name: 2013-01-02 00:00:00, dtype: float64
@@ -501,14 +501,14 @@ Name: 2013-01-02 00:00:00, dtype: float64
In [25]:
```
```py
df.loc[dates[0],'B']
```
Out[25]:
```
```py
-0.86165751902832299
```
@@ -516,7 +516,7 @@ Out[25]:
In [26]:
```
```py
%timeit -n100 df.loc[dates[0],'B']
%timeit -n100 df.at[dates[0],'B']
@@ -524,7 +524,7 @@ print df.at[dates[0],'B']
```
```
```py
100 loops, best of 3: 329 µs per loop
100 loops, best of 3: 31.1 µs per loop
-0.861657519028
@@ -537,14 +537,14 @@ print df.at[dates[0],'B']
In [27]:
```
```py
df.iloc[3]
```
Out[27]:
```
```py
A 1.289305
B 0.497115
C -0.225351
@@ -556,7 +556,7 @@ Name: 2013-01-04 00:00:00, dtype: float64
In [28]:
```
```py
df.iloc[3:5,0:2]
```
@@ -572,7 +572,7 @@ Out[28]:
In [29]:
```
```py
df.iloc[[1,2,4],[0,2]]
```
@@ -589,7 +589,7 @@ Out[29]:
In [30]:
```
```py
df.iloc[1:3,:]
```
@@ -605,7 +605,7 @@ Out[30]:
In [31]:
```
```py
df.iloc[:, 1:3]
```
@@ -625,14 +625,14 @@ Out[31]:
In [32]:
```
```py
df.iloc[1,1]
```
Out[32]:
```
```py
0.3883378290420279
```
@@ -640,7 +640,7 @@ Out[32]:
In [33]:
```
```py
%timeit -n100 df.iloc[1,1]
%timeit -n100 df.iat[1,1]
@@ -648,7 +648,7 @@ df.iat[1,1]
```
```
```py
100 loops, best of 3: 236 µs per loop
100 loops, best of 3: 14.5 µs per loop
@@ -656,7 +656,7 @@ df.iat[1,1]
Out[33]:
```
```py
0.3883378290420279
```
@@ -666,7 +666,7 @@ Out[33]:
In [34]:
```
```py
df[df.A > 0]
```
@@ -683,7 +683,7 @@ Out[34]:
In [35]:
```
```py
df[df > 0]
```
@@ -703,7 +703,7 @@ Out[35]:
In [36]:
```
```py
df2 = df.copy()
df2['E'] = ['one', 'one','two','three','four','three']
@@ -724,7 +724,7 @@ Out[36]:
In [37]:
```
```py
df2[df2['E'].isin(['two','four'])]
```
@@ -740,7 +740,7 @@ Out[37]:
In [38]:
```
```py
s1 = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6))
s1
@@ -749,7 +749,7 @@ s1
Out[38]:
```
```py
2013-01-02 1
2013-01-03 2
2013-01-04 3
@@ -763,7 +763,7 @@ Freq: D, dtype: int64
In [39]:
```
```py
df['F'] = s1
df
@@ -785,7 +785,7 @@ Out[39]:
In [40]:
```
```py
df.at[dates[0],'A'] = 0
df
@@ -805,7 +805,7 @@ Out[40]:
In [41]:
```
```py
df.iat[0, 1] = 0
df
@@ -827,7 +827,7 @@ Out[41]:
In [42]:
```
```py
df.loc[:,'D'] = np.array([5] * len(df))
df
@@ -849,7 +849,7 @@ Out[42]:
In [43]:
```
```py
df2 = df.copy()
df2[df2 > 0] = -df2
@@ -873,7 +873,7 @@ Out[43]:
In [44]:
```
```py
df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])
df1.loc[dates[0]:dates[1],'E'] = 1
@@ -894,7 +894,7 @@ Out[44]:
In [45]:
```
```py
df1.dropna(how='any')
```
@@ -909,7 +909,7 @@ Out[45]:
In [46]:
```
```py
df1.fillna(value=5)
```
@@ -927,7 +927,7 @@ Out[46]:
In [47]:
```
```py
pd.isnull(df1)
```
@@ -949,14 +949,14 @@ Out[47]:
In [48]:
```
```py
df.mean()
```
Out[48]:
```
```py
A -0.156012
B 0.023693
C 0.047490
@@ -969,14 +969,14 @@ dtype: float64
In [49]:
```
```py
df.mean(1)
```
Out[49]:
```
```py
2013-01-01 0.999519
2013-01-02 1.482023
2013-01-03 0.834970
@@ -990,14 +990,14 @@ Freq: D, dtype: float64
In [50]:
```
```py
s = pd.Series([1,3,5,np.nan,6,8], index=dates).shift(2)
print s
```
```
```py
2013-01-01 NaN
2013-01-02 NaN
2013-01-03 1
@@ -1012,7 +1012,7 @@ Freq: D, dtype: float64
In [51]:
```
```py
df.sub(s, axis='index')
```
@@ -1034,7 +1034,7 @@ Out[51]:
In [52]:
```
```py
df.apply(np.cumsum)
```
@@ -1054,14 +1054,14 @@ Out[52]:
In [53]:
```
```py
df.apply(lambda x: x.max() - x.min())
```
Out[53]:
```
```py
A 3.452758
B 2.483131
C 2.982217
@@ -1074,13 +1074,13 @@ dtype: float64
In [54]:
```
```py
s = pd.Series(np.random.randint(0, 7, size=10))
print s
```
```
```py
0 2
1 5
2 6
@@ -1099,12 +1099,12 @@ dtype: int64
In [55]:
```
```py
print s.value_counts()
```
```
```py
6 3
5 2
4 2
@@ -1119,7 +1119,7 @@ dtype: int64
In [56]:
```
```py
h = s.hist()
```
@@ -1230,14 +1230,14 @@ AwAAAABJRU5ErkJggg==
In [57]:
```
```py
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
print s.str.lower()
```
```
```py
0 a
1 b
2 c
@@ -1257,7 +1257,7 @@ dtype: object
In [58]:
```
```py
df = pd.DataFrame(np.random.randn(10, 4))
df
@@ -1283,7 +1283,7 @@ Out[58]:
In [59]:
```
```py
pieces = [df[:2], df[4:5], df[7:]]
pd.concat(pieces)
@@ -1307,7 +1307,7 @@ Out[59]:
In [60]:
```
```py
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
@@ -1316,7 +1316,7 @@ print right
```
```
```py
key lval
0 foo 1
1 foo 2
@@ -1328,7 +1328,7 @@ print right
In [61]:
```
```py
pd.merge(left, right, on='key')
```
@@ -1348,7 +1348,7 @@ Out[61]:
In [62]:
```
```py
df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
df
@@ -1372,7 +1372,7 @@ Out[62]:
In [63]:
```
```py
s = df.iloc[3]
df.append(s, ignore_index=True)
@@ -1397,7 +1397,7 @@ Out[63]:
In [64]:
```
```py
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three',
@@ -1426,7 +1426,7 @@ Out[64]:
In [65]:
```
```py
df.groupby('A').sum()
```
@@ -1444,7 +1444,7 @@ Out[65]:
In [66]:
```
```py
df.groupby(['A', 'B']).sum()
```
@@ -1470,7 +1470,7 @@ Out[66]:
In [67]:
```
```py
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz',
'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two',
@@ -1502,7 +1502,7 @@ Out[67]:
In [68]:
```
```py
df2 = df[:4]
stacked = df2.stack()
@@ -1513,7 +1513,7 @@ stacked
Out[68]:
```
```py
first second
bar one A -0.109174
B 0.958551
@@ -1530,7 +1530,7 @@ dtype: float64
In [69]:
```
```py
stacked.unstack()
```
@@ -1550,7 +1550,7 @@ Out[69]:
In [70]:
```
```py
stacked.unstack(1)
```
@@ -1572,7 +1572,7 @@ Out[70]:
In [71]:
```
```py
rng = pd.date_range('3/6/2012 00:00', periods=5, freq='D')
ts = pd.Series(np.random.randn(len(rng)), rng)
@@ -1582,7 +1582,7 @@ ts
Out[71]:
```
```py
2012-03-06 1.096788
2012-03-07 0.029678
2012-03-08 0.511461
@@ -1595,7 +1595,7 @@ Freq: D, dtype: float64
In [72]:
```
```py
ts_utc = ts.tz_localize('UTC')
ts_utc
@@ -1604,7 +1604,7 @@ ts_utc
Out[72]:
```
```py
2012-03-06 00:00:00+00:00 1.096788
2012-03-07 00:00:00+00:00 0.029678
2012-03-08 00:00:00+00:00 0.511461
@@ -1619,14 +1619,14 @@ In [ ]:
In [73]:
```
```py
ts_utc.tz_convert('US/Eastern')
```
Out[73]:
```
```py
2012-03-05 19:00:00-05:00 1.096788
2012-03-06 19:00:00-05:00 0.029678
2012-03-07 19:00:00-05:00 0.511461
@@ -1639,7 +1639,7 @@ Freq: D, dtype: float64
In [74]:
```
```py
df = pd.DataFrame({"id":[1,2,3,4,5,6], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
df
@@ -1661,7 +1661,7 @@ Out[74]:
In [75]:
```
```py
df["grade"] = df["raw_grade"].astype("category")
df["grade"]
@@ -1670,7 +1670,7 @@ df["grade"]
Out[75]:
```
```py
0 a
1 b
2 b
@@ -1685,7 +1685,7 @@ Categories (3, object): [a, b, e]
In [76]:
```
```py
df["grade"].cat.categories = ["very good", "good", "very bad"]
df["grade"]
@@ -1694,7 +1694,7 @@ df["grade"]
Out[76]:
```
```py
0 very good
1 good
2 good
@@ -1709,7 +1709,7 @@ Categories (3, object): [very good, good, very bad]
In [77]:
```
```py
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium", "good", "very good"])
df["grade"]
@@ -1717,7 +1717,7 @@ df["grade"]
Out[77]:
```
```py
0 very good
1 good
2 good
@@ -1732,14 +1732,14 @@ Categories (5, object): [very bad, bad, medium, good, very good]
In [78]:
```
```py
df.groupby("grade").size()
```
Out[78]:
```
```py
grade
very bad 1
bad 0
@@ -1755,7 +1755,7 @@ dtype: int64
In [79]:
```
```py
plt.style.use('ggplot')
```
@@ -1764,7 +1764,7 @@ plt.style.use('ggplot')
In [80]:
```
```py
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
p = ts.cumsum().plot()
@@ -2069,7 +2069,7 @@ hIhSEuCFECJKSYAXQogoJQFeCCGilAR4IYSIUv8f8P777wHo2GwAAAAASUVORK5CYII=
In [81]:
```
```py
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
columns=['A', 'B', 'C', 'D'])
@@ -2698,7 +2698,7 @@ AGMIPwAY8wFNd7R2zEaFBAAAAABJRU5ErkJggg==
In [82]:
```
```py
df.to_csv('foo.csv')
```
@@ -2707,7 +2707,7 @@ df.to_csv('foo.csv')
In [83]:
```
```py
pd.read_csv('foo.csv').head()
```
@@ -2728,7 +2728,7 @@ Out[83]:
In [84]:
```
```py
df.to_hdf("foo.h5", "df")
```
@@ -2737,7 +2737,7 @@ df.to_hdf("foo.h5", "df")
In [85]:
```
```py
pd.read_hdf('foo.h5','df').head()
```
@@ -2758,7 +2758,7 @@ Out[85]:
In [86]:
```
```py
df.to_excel('foo.xlsx', sheet_name='Sheet1')
```
@@ -2767,7 +2767,7 @@ df.to_excel('foo.xlsx', sheet_name='Sheet1')
In [87]:
```
```py
pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA']).head()
```
@@ -2786,7 +2786,7 @@ Out[87]:
In [88]:
```
```py
import glob
import os