‘‘‘
Numpy 和 Pandas 有什么不同
如果用 python 的列表和字典来作比较, 那么可以说 Numpy 是列表形式的,没有数值标签,而 Pandas 就是字典形式。Pandas是基于Numpy构建的,让Numpy为中心的应用变得更加简单。
要使用pandas,首先需要了解他主要两个数据结构:Series和DataFrame。
‘‘‘
#todo 可以说 Numpy 是列表形式的,没有数值标签,而 Pandas 就是字典形式!!
import pandas as pd
import numpy as np
s = pd.Series([1,3,6,np.nan,44,1])#输入的参数是一个列表
#此时s(series)包含了三个部分 1.索引 2.数据 3.数据类型dtype
print(s)
"""
0 1.0
1 3.0
2 6.0
3 NaN
4 44.0
5 1.0
dtype: float64
"""
# DataFrame
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.253065 -2.071051 -0.640515 0.613663
2016-01-02 -1.147178 1.532470 0.989255 -0.499761
2016-01-03 1.221656 -2.390171 1.862914 0.778070
2016-01-04 1.473877 -0.046419 0.610046 0.204672
2016-01-05 -1.584752 -0.700592 1.487264 -1.778293
2016-01-06 0.633675 -1.414157 -0.277066 -0.442545
"""
# DataFrame是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等)。DataFrame既有行索引也有列索引, 它可以被看做由Series组成的大字典。
# 我们可以根据每一个不同的索引来挑选数据, 比如挑选 b 的元素:
# DataFrame 的一些简单运用
print(df[‘b‘])#挑选b的元素
#此时返回的4各参数 是 1.行索引 2.数值 3.FREQ(频率)?? 4.dtype
"""
2016-01-01 -2.071051
2016-01-02 1.532470
2016-01-03 -2.390171
2016-01-04 -0.046419
2016-01-05 -0.700592
2016-01-06 -1.414157
Freq: D, Name: b, dtype: float64
"""
# 我们在创建一组没有给定行标签和列标签的数据 df1:
df1 = pd.DataFrame(np.arange(12).reshape((3,4)))
print(df1)
#此时没有给定index和column 默认返回0开始的索引
"""
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
"""
# 这种方法能对每一列的数据进行特殊对待. 如果想要查看数据中的类型, 我们可以用 dtype 这个属性:
# 这个相当于Excel的表格?
print(df2.dtypes)#使用df2.dtype查看每一行的数据类型
"""
df2.dtypes
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object
"""
print(df2.index)
# 如果想看对列的序号: 相当于行号(行的名称)
# Int64Index([0, 1, 2, 3], dtype=‘int64‘)
print(df2.columns)#相当于查看列的名称
# Index([‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘], dtype=‘object‘)
print(df2.values)#只返回df2的所有值,不返回行号和列号
"""
array([[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‘]], dtype=object)
"""
# 想知道数据的总结, 可以用 describe():
df2.describe()#使用describe相当于打个总结 返回count mean
"""
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
"""
print(df2.T) #转置数据 反转数据
#对数据的index(也就是行号)进行排序并且输出
print(df2.sort_index(axis=1, ascending=False)) #ascending 上升
"""
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=‘B‘))
"""
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
"""
出处:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/3-1-pd-intro/
pandas主要的两个数据结构series,dataframe
可以说numpy是列表形式的,没有数据标签,pandas是字典类型的,表格形式的dateframe!!
s = pd.Series([1,3,6,np.nan,44,1])#输入的参数是一个列表 输出包含了三个部分 1.索引 2.数据 3.数据类型dtype
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=[‘a‘,‘b‘,‘c‘,‘d‘]) 相当于生成了一个表格,行是index 列是columns
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‘})
这是创建的第二种形式,可以看到是按照每列每列的建造,非常的方便啊!在训练神经结构的时候对数据处理的时候起到了很大的作用
dateframe.index 返回行的名称
dateframe.columes 返回列的名称
dateframe.values 返回的只有值
dateframe.describe 打个总结,在预测问题上,对生成的数据进行总结
dateframe.T 转置翻转数据
dateframe.sort_index
dateframe.sort_values 都可以起到排序的作用
原文:https://www.cnblogs.com/simon-idea/p/9571381.html