rfcd12 发表于 2015-10-26 13:22:57

python pandas10分钟入门


This is a short introduction to pandas, geared mainly for new users.

Customarily, we import as follows

In : import pandas as pd
In : import numpy as np



Object Creation

See the Data Structure Intro section
Creating a Series by passing a list of values, letting pandas create a default integer index

In : s = pd.Series([1,3,5,np.nan,6,8])
In : s
Out:
0   1
1   3
2   5
3   NaN
4   6
5   8
dtype: float64

Creating a DataFrame by passing a numpy array, with a datetime index and labeled columns.

In : dates = pd.date_range('20130101',periods=6)
In : dates
Out:
<class 'pandas.tseries.index.DatetimeIndex'>

Length: 6, Freq: D, Timezone: None
In : df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
In : df
Out:
                   A         B         C         D
2013-01-010.469112 -0.282863 -1.509059 -1.135632
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.4949291.071804
2013-01-040.721555 -0.706771 -1.0395750.271860
2013-01-05 -0.4249720.5670200.276232 -1.087401
2013-01-06 -0.6736900.113648 -1.4784270.524988

Creating a DataFrame by passing a dict of objects that can be converted to series-like.

In : df2 = pd.DataFrame({ 'A' : 1.,
   ...:                      'B' : pd.Timestamp('20130102'),
   ...:                      'C' : pd.Series(1,index=range(4),dtype='float32'),
   ...:                      'D' : np.array([3] * 4,dtype='int32'),
   ...:                      'E' : 'foo' })
   ...:
In : df2
Out:
   A                   BCD    E
01 2013-01-02 00:00:0013foo
11 2013-01-02 00:00:0013foo
21 2013-01-02 00:00:0013foo
31 2013-01-02 00:00:0013foo

Having specific dtypes

In : df2.dtypes
Out:
A         float64
B    datetime64
C         float32
D             int32
E            object
dtype: object



Viewing Data

See the Basics section
See the top & bottom rows of the frame

In : df.head()
Out:
                   A         B         C         D
2013-01-010.469112 -0.282863 -1.509059 -1.135632
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.4949291.071804
2013-01-040.721555 -0.706771 -1.0395750.271860
2013-01-05 -0.4249720.5670200.276232 -1.087401
In : df.tail(3)
Out:
                   A         B         C         D
2013-01-040.721555 -0.706771 -1.0395750.271860
2013-01-05 -0.4249720.5670200.276232 -1.087401
2013-01-06 -0.6736900.113648 -1.4784270.524988

Display the index,columns, and the underlying numpy data

In : df.index
Out:
<class 'pandas.tseries.index.DatetimeIndex'>

Length: 6, Freq: D, Timezone: None
In : df.columns
Out: Index(, dtype=object)
In : df.values
Out:
array([[ 0.4691, -0.2829, -1.5091, -1.1356],
       [ 1.2121, -0.1732,0.1192, -1.0442],
       [-0.8618, -2.1046, -0.4949,1.0718],
       [ 0.7216, -0.7068, -1.0396,0.2719],
       [-0.425 ,0.567 ,0.2762, -1.0874],
       [-0.6737,0.1136, -1.4784,0.525 ]])

Describe shows a quick statistic summary of your data

In : df.describe()
Out:
            A         B         C         D
count6.0000006.0000006.0000006.000000
mean   0.073711 -0.431125 -0.687758 -0.233103
std    0.8431570.9228180.7798870.973118
min   -0.861849 -2.104569 -1.509059 -1.135632
25%   -0.611510 -0.600794 -1.368714 -1.076610
50%    0.022070 -0.228039 -0.767252 -0.386188
75%    0.6584440.041933 -0.0343260.461706
max    1.2121120.5670200.2762321.071804

Transposing your data

In : df.T
Out:
   2013-01-012013-01-022013-01-032013-01-042013-01-052013-01-06
A    0.469112    1.212112   -0.861849    0.721555   -0.424972   -0.673690
B   -0.282863   -0.173215   -2.104569   -0.706771    0.567020    0.113648
C   -1.509059    0.119209   -0.494929   -1.039575    0.276232   -1.478427
D   -1.135632   -1.044236    1.071804    0.271860   -1.087401    0.524988

Sorting by an axis

In : df.sort_index(axis=1, ascending=False)
Out:
                   D         C         B         A
2013-01-01 -1.135632 -1.509059 -0.2828630.469112
2013-01-02 -1.0442360.119209 -0.1732151.212112
2013-01-031.071804 -0.494929 -2.104569 -0.861849
2013-01-040.271860 -1.039575 -0.7067710.721555
2013-01-05 -1.0874010.2762320.567020 -0.424972
2013-01-060.524988 -1.4784270.113648 -0.673690

Sorting by values

In : df.sort(columns='B')
Out:
                   A         B         C         D
2013-01-03 -0.861849 -2.104569 -0.4949291.071804
2013-01-040.721555 -0.706771 -1.0395750.271860
2013-01-010.469112 -0.282863 -1.509059 -1.135632
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-06 -0.6736900.113648 -1.4784270.524988
2013-01-05 -0.4249720.5670200.276232 -1.087401



Selection


Note

While standard Python / Numpy expressions for selecting and setting are intuitive and come in handy for interactive work, for production code, we recommend the optimized pandas data access methods, .at, .iat, .loc, .iloc and .ix.

See the Indexing section and below.


Getting

Selecting a single column, which yields a Series, equivalent to df.A

In : df['A']
Out:
2013-01-01    0.469112
2013-01-02    1.212112
2013-01-03   -0.861849
2013-01-04    0.721555
2013-01-05   -0.424972
2013-01-06   -0.673690
Freq: D, Name: A, dtype: float64

Selecting via [], which slices the rows.

In : df[0:3]
Out:
                   A         B         C         D
2013-01-010.469112 -0.282863 -1.509059 -1.135632
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.4949291.071804
In : df['20130102':'20130104']
Out:
                   A         B         C         D
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.4949291.071804
2013-01-040.721555 -0.706771 -1.0395750.271860



Selection by Label

See more in Selection by Label
For getting a cross section using a label

In : df.loc0]]
Out:
A    0.469112
B   -0.282863
C   -1.509059
D   -1.135632
Name: 2013-01-01 00:00:00, dtype: float64

Selecting on a multi-axis by label

In : df.loc[:,['A','B']]
Out:
                   A         B
2013-01-010.469112 -0.282863
2013-01-021.212112 -0.173215
2013-01-03 -0.861849 -2.104569
2013-01-040.721555 -0.706771
2013-01-05 -0.4249720.567020
2013-01-06 -0.6736900.113648

Showing label slicing, both endpoints are included

In : df.loc['20130102':'20130104',['A','B']]
Out:
                   A         B
2013-01-021.212112 -0.173215
2013-01-03 -0.861849 -2.104569
2013-01-040.721555 -0.706771

Reduction in the dimensions of the returned object

In : df.loc['20130102',['A','B']]
Out:
A    1.212112
B   -0.173215
Name: 2013-01-02 00:00:00, dtype: float64

For getting a scalar value

In : df.loc0],'A']
Out: 0.46911229990718628

For getting fast access to a scalar (equiv to the prior method)

In : df.at0],'A']
Out: 0.46911229990718628



Selection by Position

See more in Selection by Position
Select via the position of the passed integers

In : df.iloc[3]
Out:
A    0.721555
B   -0.706771
C   -1.039575
D    0.271860
Name: 2013-01-04 00:00:00, dtype: float64

By integer slices, acting similar to numpy/python

In : df.iloc[3:5,0:2]
Out:
                   A         B
2013-01-040.721555 -0.706771
2013-01-05 -0.4249720.567020

By lists of integer position locations, similar to the numpy/python style

In : df.iloc[[1,2,4],[0,2]]
Out:
                   A         C
2013-01-021.2121120.119209
2013-01-03 -0.861849 -0.494929
2013-01-05 -0.4249720.276232

For slicing rows explicitly

In : df.iloc[1:3,:]
Out:
                   A         B         C         D
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.4949291.071804

For slicing columns explicitly

In : df.iloc[:,1:3]
Out:
                   B         C
2013-01-01 -0.282863 -1.509059
2013-01-02 -0.1732150.119209
2013-01-03 -2.104569 -0.494929
2013-01-04 -0.706771 -1.039575
2013-01-050.5670200.276232
2013-01-060.113648 -1.478427

For getting a value explicity

In : df.iloc[1,1]
Out: -0.17321464905330858

For getting fast access to a scalar (equiv to the prior method)

In : df.iat[1,1]
Out: -0.17321464905330858

There is one signficant departure from standard python/numpy slicing semantics. python/numpy allow slicing past the end of an array without an associated error.

# these are allowed in python/numpy.
In : x = list('abcdef')
In : x[4:10]
Out: ['e', 'f']
In : x[8:10]
Out: []

Pandas will detect this and raise IndexError, rather than return an empty structure.

>>> df.iloc[:,8:10]
IndexError: out-of-bounds on slice (end)



Boolean Indexing

Using a single column’s values to select data.

In : df.A > 0]
Out:
                   A         B         C         D
2013-01-010.469112 -0.282863 -1.509059 -1.135632
2013-01-021.212112 -0.1732150.119209 -1.044236
2013-01-040.721555 -0.706771 -1.0395750.271860

A where operation for getting.

In : df> 0]
Out:
                   A         B         C         D
2013-01-010.469112       NaN       NaN       NaN
2013-01-021.212112       NaN0.119209       NaN
2013-01-03       NaN       NaN       NaN1.071804
2013-01-040.721555       NaN       NaN0.271860
2013-01-05       NaN0.5670200.276232       NaN
2013-01-06       NaN0.113648       NaN0.524988



Setting

Setting a new column automatically aligns the data by the indexes

In : s1 = pd.Series([1,2,3,4,5,6],index=date_range('20130102',periods=6))
In : s1
Out:
2013-01-02    1
2013-01-03    2
2013-01-04    3
2013-01-05    4
2013-01-06    5
2013-01-07    6
Freq: D, dtype: int64
In : df['F'] = s1

Setting values by label

In : df.at0],'A'] = 0

Setting values by position

In : df.iat[0,1] = 0

Setting by assigning with a numpy array

In : df.loc[:,'D'] = np.array([5] * len(df))

The result of the prior setting operations

In : df
Out:
                   A         B         CD   F
2013-01-010.0000000.000000 -1.5090595 NaN
2013-01-021.212112 -0.1732150.1192095   1
2013-01-03 -0.861849 -2.104569 -0.4949295   2
2013-01-040.721555 -0.706771 -1.0395755   3
2013-01-05 -0.4249720.5670200.2762325   4
2013-01-06 -0.6736900.113648 -1.4784275   5

A where operation with setting.

In : df2 = df.copy()
In : df2> 0] = -df2
In : df2
Out:
                   A         B         CD   F
2013-01-010.0000000.000000 -1.509059 -5 NaN
2013-01-02 -1.212112 -0.173215 -0.119209 -5-1
2013-01-03 -0.861849 -2.104569 -0.494929 -5-2
2013-01-04 -0.721555 -0.706771 -1.039575 -5-3
2013-01-05 -0.424972 -0.567020 -0.276232 -5-4
2013-01-06 -0.673690 -0.113648 -1.478427 -5-5



Missing Data

Pandas primarily uses the value np.nan to represent missing data. It is by default not included
in computations. See the Missing Data section
Reindexing allows you to change/add/delete the index on a specified axis. This returns a copy of the data.

In : df1 = df.reindex(index=dates[0:4],columns=list(df.columns) &#43; ['E'])
In : df1.loc0]:dates[1],'E'] = 1
In : df1
Out:
                   A         B         CD   F   E
2013-01-010.0000000.000000 -1.5090595 NaN   1
2013-01-021.212112 -0.1732150.1192095   1   1
2013-01-03 -0.861849 -2.104569 -0.4949295   2 NaN
2013-01-040.721555 -0.706771 -1.0395755   3 NaN

To drop any rows that have missing data.

In : df1.dropna(how='any')
Out:
                   A         B         CDFE
2013-01-021.212112 -0.1732150.119209511

Filling missing data

In : df1.fillna(value=5)
Out:
                   A         B         CDFE
2013-01-010.0000000.000000 -1.509059551
2013-01-021.212112 -0.1732150.119209511
2013-01-03 -0.861849 -2.104569 -0.494929525
2013-01-040.721555 -0.706771 -1.039575535

To get the boolean mask where values are nan

In : pd.isnull(df1)
Out:
                A      B      C      D      F      E
2013-01-01FalseFalseFalseFalse   TrueFalse
2013-01-02FalseFalseFalseFalseFalseFalse
2013-01-03FalseFalseFalseFalseFalse   True
2013-01-04FalseFalseFalseFalseFalse   True



Operations

See the Basic section on Binary Ops


Stats

Operations in general exclude missing data.
Performing a descriptive statistic

In : df.mean()
Out:
A   -0.004474
B   -0.383981
C   -0.687758
D    5.000000
F    3.000000
dtype: float64

Same operation on the other axis

In : df.mean(1)
Out:
2013-01-01    0.872735
2013-01-02    1.431621
2013-01-03    0.707731
2013-01-04    1.395042
2013-01-05    1.883656
2013-01-06    1.592306
Freq: D, dtype: float64

Operating with objects that have different dimensionality and need alignment. In addition, pandas automatically broadcasts along the specified dimension.

In : s = pd.Series([1,3,5,np.nan,6,8],index=dates).shift(2)
In : s
Out:
2013-01-01   NaN
2013-01-02   NaN
2013-01-03   1
2013-01-04   3
2013-01-05   5
2013-01-06   NaN
Freq: D, dtype: float64
In : df.sub(s,axis='index')
Out:
                   A         B         C   D   F
2013-01-01       NaN       NaN       NaN NaN NaN
2013-01-02       NaN       NaN       NaN NaN NaN
2013-01-03 -1.861849 -3.104569 -1.494929   4   1
2013-01-04 -2.278445 -3.706771 -4.039575   2   0
2013-01-05 -5.424972 -4.432980 -4.723768   0-1
2013-01-06       NaN       NaN       NaN NaN NaN



Apply

Applying functions to the data

In : df.apply(np.cumsum)
Out:
                   A         B         C   D   F
2013-01-010.0000000.000000 -1.509059   5 NaN
2013-01-021.212112 -0.173215 -1.38985010   1
2013-01-030.350263 -2.277784 -1.88477915   3
2013-01-041.071818 -2.984555 -2.92435420   6
2013-01-050.646846 -2.417535 -2.6481222510
2013-01-06 -0.026844 -2.303886 -4.1265493015
In : df.apply(lambda x: x.max() - x.min())
Out:
A    2.073961
B    2.671590
C    1.785291
D    0.000000
F    4.000000
dtype: float64



Histogramming

See more at Histogramming and Discretization

In : s = Series(np.random.randint(0,7,size=10))
In : s
Out:
0    4
1    2
2    1
3    2
4    6
5    4
6    4
7    6
8    4
9    4
dtype: int64
In : s.value_counts()
Out:
4    5
6    2
2    2
1    1
dtype: int64



String Methods

See more at Vectorized String Methods

In : s = Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
In : s.str.lower()
Out:
0       a
1       b
2       c
3    aaba
4    baca
5   NaN
6    caba
7   dog
8   cat
dtype: object



Merge


Concat

Pandas provides various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations.
See the Merging section
Concatenating pandas objects together

In : df = pd.DataFrame(np.random.randn(10, 4))
In : df
Out:
          0         1         2         3
0 -0.5487021.467327 -1.015962 -0.483075
11.637550 -1.217659 -0.291519 -1.745505
2 -0.2639520.991460 -0.9190690.266046
3 -0.7096611.6690521.037882 -1.705775
4 -0.919854 -0.0423791.247642 -0.009920
50.2902130.4957670.3629491.548106
6 -1.131345 -0.0893290.337863 -0.945867
7 -0.9321321.9560300.017587 -0.016692
8 -0.5752470.254161 -1.1437040.215897
91.193555 -0.077118 -0.408530 -0.862495
# break it into pieces
In : pieces = 3], df[3:7], df[7:]]
In : concat(pieces)
Out:
          0         1         2         3
0 -0.5487021.467327 -1.015962 -0.483075
11.637550 -1.217659 -0.291519 -1.745505
2 -0.2639520.991460 -0.9190690.266046
3 -0.7096611.6690521.037882 -1.705775
4 -0.919854 -0.0423791.247642 -0.009920
50.2902130.4957670.3629491.548106
6 -1.131345 -0.0893290.337863 -0.945867
7 -0.9321321.9560300.017587 -0.016692
8 -0.5752470.254161 -1.1437040.215897
91.193555 -0.077118 -0.408530 -0.862495



Join

SQL style merges. See the Database style joining

In : left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
In : right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
In : left
Out:
   keylval
0foo   1
1foo   2
In : right
Out:
   keyrval
0foo   4
1foo   5
In : merge(left, right, on='key')
Out:
   keylvalrval
0foo   1   4
1foo   1   5
2foo   2   4
3foo   2   5



Append

Append rows to a dataframe. See the Appending

In : df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
In : df
Out:
          A         B         C         D
01.3460611.5117631.627081 -0.990582
1 -0.4416521.2115260.2685200.024580
2 -1.5775850.396823 -0.105381 -0.532532
31.4537491.208843 -0.080952 -0.264610
4 -0.727965 -0.5893460.339969 -0.693205
5 -0.3393550.5936160.8843451.591431
60.1418090.2203900.4355890.192451
7 -0.0967010.8033511.715071 -0.708758
In : s = df.iloc[3]
In : df.append(s, ignore_index=True)
Out:
          A         B         C         D
01.3460611.5117631.627081 -0.990582
1 -0.4416521.2115260.2685200.024580
2 -1.5775850.396823 -0.105381 -0.532532
31.4537491.208843 -0.080952 -0.264610
4 -0.727965 -0.5893460.339969 -0.693205
5 -0.3393550.5936160.8843451.591431
60.1418090.2203900.4355890.192451
7 -0.0967010.8033511.715071 -0.708758
81.4537491.208843 -0.080952 -0.264610



Grouping

By “group by” we are referring to a process involving one or more of the following steps



[*]Splitting the data into groups based on some criteria
[*]Applying a function to each group independently
[*]Combining the results into a data structure


See the Grouping section

In : df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
   ....:                        'foo', 'bar', 'foo', 'foo'],
   ....:                  'B' : ['one', 'one', 'two', 'three',
   ....:                        'two', 'two', 'one', 'three'],
   ....:                  'C' : randn(8), 'D' : randn(8)})
   ....:
In : df
Out:
   A      B         C         D
0foo    one -1.202872 -0.055224
1bar    one -1.8144702.395985
2foo    two1.0186011.552825
3barthree -0.5954470.166599
4foo    two1.3954330.047609
5bar    two -0.392670 -0.136473
6foo    one0.007207 -0.561757
7foothree1.928123 -1.623033

Grouping and then applying a function sum to the resulting groups.

In : df.groupby('A').sum()
Out:
            C      D
A                     
bar -2.8025882.42611
foo3.146492 -0.63958

Grouping by multiple columns forms a hierarchical index, which we then apply the function.

In : df.groupby(['A','B']).sum()
Out:
                  C         D
A   B                        
bar one   -1.8144702.395985
    three -0.5954470.166599
    two   -0.392670 -0.136473
foo one   -1.195665 -0.616981
    three1.928123 -1.623033
    two    2.4140341.600434



Reshaping

See the section on Hierarchical Indexing and see
the section on Reshaping).


Stack

In : tuples = zip(*[['bar', 'bar', 'baz', 'baz',
   ....:               'foo', 'foo', 'qux', 'qux'],
   ....:                ['one', 'two', 'one', 'two',
   ....:               'one', 'two', 'one', 'two']])
   ....:
In : index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
In : df = pd.DataFrame(randn(8, 2), index=index, columns=['A', 'B'])
In : df2 = df[:4]
In : df2
Out:
                     A         B
first second                  
bar   one   0.029399 -0.542108
      two   0.282696 -0.087302
baz   one    -1.5751701.771208
      two   0.8164821.100230

The stack function “compresses” a level in the DataFrame’s columns.

In : stacked = df2.stack()
In : stacked
Out:
firstsecond   
bar    one   A    0.029399
               B   -0.542108
       two   A    0.282696
               B   -0.087302
baz    one   A   -1.575170
               B    1.771208
       two   A    0.816482
               B    1.100230
dtype: float64

With a “stacked” DataFrame or Series (having a MultiIndex as the index),
the inverse operation of stack is unstack,
which by default unstacks the last level:

In : stacked.unstack()
Out:
                     A         B
first second                  
bar   one   0.029399 -0.542108
      two   0.282696 -0.087302
baz   one    -1.5751701.771208
      two   0.8164821.100230
In : stacked.unstack(1)
Out:
second      one       two
first                     
bar   A0.0293990.282696
      B -0.542108 -0.087302
baz   A -1.5751700.816482
      B1.7712081.100230
In : stacked.unstack(0)
Out:
first          bar       baz
second                     
one    A0.029399 -1.575170
       B -0.5421081.771208
two    A0.2826960.816482
       B -0.0873021.100230



Pivot Tables

See the section on Pivot Tables.

In : df = DataFrame({'A' : ['one', 'one', 'two', 'three'] * 3,
   ....:               'B' : ['A', 'B', 'C'] * 4,
   ....:               'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
   ....:               'D' : np.random.randn(12),
   ....:               'E' : np.random.randn(12)})
   ....:
In : df
Out:
      AB    C         D         E
0   oneAfoo1.418757 -0.179666
1   oneBfoo -1.8790241.291836
2   twoCfoo0.536826 -0.009614
3   threeAbar1.0061600.392149
4   oneBbar -0.0297160.264599
5   oneCbar -1.146178 -0.057409
6   twoAfoo0.100900 -1.425638
7   threeBfoo -1.0350181.024098
8   oneCfoo0.314665 -0.106062
9   oneAbar -0.7737231.824375
10    twoBbar -1.1706530.595974
11threeCbar0.6487401.167115

We can produce pivot tables from this data very easily:

In : pivot_table(df, values='D', rows=['A', 'B'], cols=['C'])
Out:
C             bar       foo
A   B                  
one   A -0.7737231.418757
      B -0.029716 -1.879024
      C -1.1461780.314665
three A1.006160       NaN
      B       NaN -1.035018
      C0.648740       NaN
two   A       NaN0.100900
      B -1.170653       NaN
      C       NaN0.536826



Time Series

Pandas has simple, powerful, and efficient functionality for performing resampling operations during frequency conversion (e.g., converting secondly data into 5-minutely data). This is extremely common in, but not limited to, financial
applications. See the Time Series section

In : rng = pd.date_range('1/1/2012', periods=100, freq='S')
In : ts = pd.Series(randint(0, 500, len(rng)), index=rng)
In : ts.resample('5Min', how='sum')
Out:
2012-01-01    25083
Freq: 5T, dtype: int64

Time zone representation

In : rng = pd.date_range('3/6/2012 00:00', periods=5, freq='D')
In : ts = pd.Series(randn(len(rng)), rng)
In : ts_utc = ts.tz_localize('UTC')
In : ts_utc
Out:
2012-03-06 00:00:00&#43;00:00    0.464000
2012-03-07 00:00:00&#43;00:00    0.227371
2012-03-08 00:00:00&#43;00:00   -0.496922
2012-03-09 00:00:00&#43;00:00    0.306389
2012-03-10 00:00:00&#43;00:00   -2.290613
Freq: D, dtype: float64

Convert to another time zone

In : ts_utc.tz_convert('US/Eastern')
Out:
2012-03-05 19:00:00-05:00    0.464000
2012-03-06 19:00:00-05:00    0.227371
2012-03-07 19:00:00-05:00   -0.496922
2012-03-08 19:00:00-05:00    0.306389
2012-03-09 19:00:00-05:00   -2.290613
Freq: D, dtype: float64

Converting between time span representations

In : rng = pd.date_range('1/1/2012', periods=5, freq='M')
In : ts = pd.Series(randn(len(rng)), index=rng)
In : ts
Out:
2012-01-31   -1.134623
2012-02-29   -1.561819
2012-03-31   -0.260838
2012-04-30    0.281957
2012-05-31    1.523962
Freq: M, dtype: float64
In : ps = ts.to_period()
In : ps
Out:
2012-01   -1.134623
2012-02   -1.561819
2012-03   -0.260838
2012-04    0.281957
2012-05    1.523962
Freq: M, dtype: float64
In : ps.to_timestamp()
Out:
2012-01-01   -1.134623
2012-02-01   -1.561819
2012-03-01   -0.260838
2012-04-01    0.281957
2012-05-01    1.523962
Freq: MS, dtype: float64

Converting between period and timestamp enables some convenient arithmetic functions to be used. In the following example, we convert a quarterly frequency with year ending in November to 9am of the end of the month following the
quarter end:

In : prng = period_range('1990Q1', '2000Q4', freq='Q-NOV')
In : ts = Series(randn(len(prng)), prng)
In : ts.index = (prng.asfreq('M', 'e') &#43; 1).asfreq('H', 's') &#43; 9
In : ts.head()
Out:
1990-03-01 09:00   -0.902937
1990-06-01 09:00    0.068159
1990-09-01 09:00   -0.057873
1990-12-01 09:00   -0.368204
1991-03-01 09:00   -1.144073
Freq: H, dtype: float64



Plotting

Plotting docs.

In : ts = pd.Series(randn(1000), index=pd.date_range('1/1/2000', periods=1000))
In : ts = ts.cumsum()
In : ts.plot()
Out: <matplotlib.axes.AxesSubplot at 0x40d5110>


On DataFrame, plot is a convenience to plot all of the columns with labels:

In : df = pd.DataFrame(randn(1000, 4), index=ts.index,
   .....:                   columns=['A', 'B', 'C', 'D'])
   .....:
In : df = df.cumsum()
In : plt.figure(); df.plot(); plt.legend(loc='best')
Out: <matplotlib.legend.Legend at 0x4321d90>



Getting Data In/Out


CSV

Writing to a csv file

In : df.to_csv('foo.csv')

Reading from a csv file

In : pd.read_csv('foo.csv')
Out:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000 entries, 0 to 999
Data columns (total 5 columns):
Unnamed: 0    1000non-null values
A             1000non-null values
B             1000non-null values
C             1000non-null values
D             1000non-null values
dtypes: float64(4), object(1)



HDF5

Reading and writing to HDFStores
Writing to a HDF5 Store

In : df.to_hdf('foo.h5','df')

Reading from a HDF5 Store

In : read_hdf('foo.h5','df')
Out:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1000 entries, 2000-01-01 00:00:00 to 2002-09-26 00:00:00
Freq: D
Data columns (total 4 columns):
A    1000non-null values
B    1000non-null values
C    1000non-null values
D    1000non-null values
dtypes: float64(4)



Excel

Reading and writing to MS Excel
Writing to an excel file

In : df.to_excel('foo.xlsx', sheet_name='sheet1')

Reading from an excel file

In : xls = ExcelFile('foo.xlsx')
In : xls.parse('sheet1', index_col=None, na_values=['NA'])
Out:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1000 entries, 2000-01-01 00:00:00 to 2002-09-26 00:00:00
Data columns (total 4 columns):
A    1000non-null values
B    1000non-null values
C    1000non-null values
D    1000non-null values
dtypes: float64(4)

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: python pandas10分钟入门