非常推荐莫烦python,对于初学者来说,真的变得通俗易懂!友好地带你入门!
学到numpy,不可避免的要学下pandas。pandas使得numpy的使用变得非常的简单。如果把numpy比作数据库中的表,那么pandas就是对这些表进行操作的SQL语句。
首先。来了解下pandas的两个主要的数据结构:Series和DataFrame。
Series
import pandas as pd
import numpy as np
s = pd.Series([1,3,6,np.nan,44,1])
print(s)
"""
0 1.0
1 3.0
2 6.0
3 NaN
4 44.0
5 1.0
dtype: float64
"""
Series
的字符串表现形式为:索引在左边,值在右边。由于我们没有为数据指定索引。于是会自动创建一个0到N-1(N为长度)的整数型索引。
DataFrame
import pandas as pd
import numpy as np
dates = pd.date_range('20160101', periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd'])
print(df)
"""
a b c d
2016-01-01 -0.182750 0.725238 -0.609168 1.040427
2016-01-02 0.602966 -0.422427 -0.746228 1.522527
2016-01-03 -0.283916 -0.757592 1.472891 -0.195762
2016-01-04 -0.687848 -2.458511 0.370837 -0.205588
2016-01-05 -1.448794 0.521031 -0.054159 0.764528
2016-01-06 -0.702978 1.202219 -0.720881 0.635985
"""
DataFrame相比于Series,一个是一维,一个是二维。既有行索引,也有列索引。其中,每列可以是不同的值类型(数值,字符串,布尔值等)。因此,我们可以根据行索引和列索引来挑选某个具体的数值。
简单应用:
- 上述创建数据的时候给定了行标签和列标签,若什么都没给出,默认从0开始index。
df1 = pd.DataFrame(np.arange(12).reshape((3, 4)))
print(df1)
"""
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
"""
- 也可以具体指定相应行列的数据类型
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)
"""
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
"""
print(df2.dtypes)
'''
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object
'''
这种方法能对每一列的数据进行特殊对待. 如果想要查看数据中的类型, 我们可以用 dtypes
- 输出具体某一列
print(df['b'])
'''
2016-01-01 -0.150033
2016-01-02 0.446891
2016-01-03 2.010282
2016-01-04 0.970165
2016-01-05 -0.609038
2016-01-06 -0.718163
Freq: D, Name: b, dtype: float64
'''
- 输出index
print(df2.index)
# Int64Index([0, 1, 2, 3], dtype='int64')
- 输出行名称
print(df2.columns)
# Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
- 如果只想看values
print(df2.values)
'''
[[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']]
'''
- 想知道数据的总结, 可以用
describe()
print(df2.describe())
"""
A C D
count 4.0 4.0 4.0
mean 1.0 1.0 3.0
std 0.0 0.0 0.0
min 1.0 1.0 3.0
25% 1.0 1.0 3.0
50% 1.0 1.0 3.0
75% 1.0 1.0 3.0
max 1.0 1.0 3.0
"""
- 如果想翻转数据,
transpose
:
print(df2.T)
'''
0 ... 3
A 1 ... 1
B 2013-01-02 00:00:00 ... 2013-01-02 00:00:00
C 1 ... 1
D 3 ... 3
E test ... train
F foo ... foo
[6 rows x 4 columns]
'''
- 排序
print(df2.sort_index(axis=1, ascending=False))
"""
F E D C B A
0 foo test 3 1.0 2013-01-02 1.0
1 foo train 3 1.0 2013-01-02 1.0
2 foo test 3 1.0 2013-01-02 1.0
3 foo train 3 1.0 2013-01-02 1.0
"""
print(df2.sort_values(by='E'))
"""
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
2 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
3 1.0 2013-01-02 1.0 3 train foo
"""
以上为pandas中Series和DataFrame一些简单介绍和应用。可见,加入pandas后,操作变得非常的简单,易于理解。