In [58]: names=['Bob','Jessica','Mary','John','Mel']
In [59]: births = [968, 155, 77, 578, 973]
In [60]: BabyDataSet = list(zip(names,births))
...: BabyDataSet
Out[60]: [('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]
In [61]: df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births
...: '])
...: df
Out[61]:
Names Births
0 Bob 9681 Jessica 1552 Mary 773 John 5784 Mel 973
2.查看数据类型
In [64]: df.dtypes
Out[64]:
Names object
Births int64
dtype: object
In [65]: df.Names.dtypes
Out[65]: dtype('O')
In [66]: df.Births.dtypes
Out[66]: dtype('int64')
3.按照指定 的一列排序
In [68]: df.sort_values(['Births'])
Out[68]:
Names Births
2 Mary 771 Jessica 1553 John 5780 Bob 9684 Mel 973In [69]: df.sort_values(['Births'],ascending=False)
Out[69]:
Names Births
4 Mel 9730 Bob 9683 John 5781 Jessica 1552 Mary 77
4.选取前几行或者末尾几行
In [72]: df.head(3)
Out[72]:
Names Births
0 Bob 9681 Jessica 1552 Mary 77In [73]: df.tail(2)
Out[73]:
Names Births
3 John 5784 Mel 973In [74]: df
Out[74]:
Names Births
0 Bob 9681 Jessica 1552 Mary 773 John 5784 Mel 973
# encoding=utf-8import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Creating a Series by passing a list of values, letting pandas create a default integer index
s = pd.Series([1, 3, 5, np.nan, 6, 8])
print(s)
# Creating a DataFrame by passing a numpy array, with a datetime index and labeled columns:
dates = pd.date_range('20130101', periods=6)
print(dates)
# Creating a DataFrame by passing a numpy array, with a datetime index and labeled columns:
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(df)
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'})
print(df2)
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06'],
dtype='datetime64[ns]', freq='D')
A B C D
2013-01-01 -1.324399 -0.917851 -0.710650 0.977088
2013-01-02 0.034877 -0.417994 1.412711 -0.626197
2013-01-03 -0.559784 -0.085540 1.067182 0.649621
2013-01-04 0.849592 -1.251283 1.956991 1.189781
2013-01-05 -1.742392 0.193744 -0.570087 -0.277156
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
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
# 查看数据的头部和尾部# See the top & bottom rows of the frameprint"--------*---------------"
print(df2.head(1))
print(df2.tail(1))
print(df2.values)
# 排序# Sorting by an axis(轴)print"--------*---------------"# 可以看到按照列名排序了print df
print df.sort_index(axis=1, ascending=False)
# Sorting by values# 按照B列排序print df.sort_values(by='B')
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
A B C D E F
3 1.0 2013-01-02 1.0 3 train foo
[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
--------*---------------
A B C D
2013-01-01 -1.324399 -0.917851 -0.710650 0.977088
2013-01-02 0.034877 -0.417994 1.412711 -0.626197
2013-01-03 -0.559784 -0.085540 1.067182 0.649621
2013-01-04 0.849592 -1.251283 1.956991 1.189781
2013-01-05 -1.742392 0.193744 -0.570087 -0.277156
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
D C B A
2013-01-01 0.977088 -0.710650 -0.917851 -1.324399
2013-01-02 -0.626197 1.412711 -0.417994 0.034877
2013-01-03 0.649621 1.067182 -0.085540 -0.559784
2013-01-04 1.189781 1.956991 -1.251283 0.849592
2013-01-05 -0.277156 -0.570087 0.193744 -1.742392
2013-01-06 -1.298726 -1.324529 -0.890113 -0.129934
A B C D
2013-01-04 0.849592 -1.251283 1.956991 1.189781
2013-01-01 -1.324399 -0.917851 -0.710650 0.977088
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
2013-01-02 0.034877 -0.417994 1.412711 -0.626197
2013-01-03 -0.559784 -0.085540 1.067182 0.649621
2013-01-05 -1.742392 0.193744 -0.570087 -0.277156
# Selecting a single column, which yields a Series, equivalent to df.Aprint df.A
print df['A']
# Selecting via [], which slices the rows.print df[0:3]
2013-01-01 -1.324399
2013-01-02 0.034877
2013-01-03 -0.559784
2013-01-04 0.849592
2013-01-05 -1.742392
2013-01-06 -0.129934
Freq: D, Name: A, dtype: float64
2013-01-01 -1.324399
2013-01-02 0.034877
2013-01-03 -0.559784
2013-01-04 0.849592
2013-01-05 -1.742392
2013-01-06 -0.129934
Freq: D, Name: A, dtype: float64
A B C D
2013-01-01 -1.324399 -0.917851 -0.710650 0.977088
2013-01-02 0.034877 -0.417994 1.412711 -0.626197
2013-01-03 -0.559784 -0.085540 1.067182 0.649621
#选取第一行print df.loc[dates[0]]
A -1.324399
B -0.917851
C -0.710650
D 0.977088
Name: 2013-01-01 00:00:00, dtype: float64