10 Minutes to pandas
必要的库导入:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
创建对象
pandas常用数据类型有两个:series和dataframe。
创建一个series:
s = pd.Series([1,3,5,np.nan,6,8])
s
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
通过数列创建一个dataframe:
dates = pd.date_range('20130101', periods=6)
dates
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06'],
dtype='datetime64[ns]', freq='D')
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
df
A B C D
2013-01-01 0.828772 -0.681941 -0.736688 0.497738
2013-01-02 -1.744554 1.840190 1.108693 0.718830
2013-01-03 1.022257 0.956576 -1.538469 -0.097789
2013-01-04 -0.818469 0.017786 0.365621 0.687680
2013-01-05 0.418984 1.301549 1.248974 -0.712357
2013-01-06 0.949965 -0.778907 0.029515 0.200063
通过字典创建一个dataframe:
df2 = pd.DataFrame({
'A' : 1.,
....: 'B' : pd.Timestamp('20130102'),
..