Series
是一维的数据结构。
通过list构建Series
#导入pandas
import pandas as pd
ser_obj =pd.Series(range(10,15))
print(type(ser_obj)) # <class 'pandas.core.series.Series'>
print(ser_obj)
<class 'pandas.core.series.Series'>
0 10
1 11
2 12
3 13
4 14
dtype: int32
获取数据
print(type(ser_obj.values)) # <class 'numpy.ndarray'>
print(ser_obj.values) # [10 11 12 13 14]
<class 'numpy.ndarray'>
[10 11 12 13 14]
获取索引
print(type(ser_obj.index)) # <class 'pandas.core.indexes.range.RangeIndex'>
print(ser_obj.index) # RangeIndex(start=0, stop=5, step=1)
<class 'pandas.core.indexes.range.RangeIndex'>
RangeIndex(start=0, stop=5, step=1)
注意索引对象不可变
# 索引对象不可变
ser_obj.index[0] = 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-ce46badf9dd7> in <module>()
----> 1 ser_obj.index[0] = 2
G:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
1668
1669 def __setitem__(self, key, value):
-> 1670 raise TypeError("Index does not support mutable operations")
1671
1672 def __getitem__(self, key):
TypeError: Index does not support mutable operations
预览数据
print(ser_obj.head(3))
0 10
1 11
2 12
dtype: int32
通过索引获取数据
print(ser_obj[0]) # 10
10
索引与数据的对应关系仍保持在数组运算的结果中
print(ser_obj > 12)
print(ser_obj[ser_obj > 12])
0 False
1 False
2 False
3 True
4 True
dtype: bool
3 13
4 14
dtype: int32
整合代码
# 通过list构建Series
ser_obj =pd.Series(range(10,15))
print(type(ser_obj)) # <class 'pandas.core.series.Series'>
print(ser_obj)
# 获取数据
print(type(ser_obj.values)) # <class 'numpy.ndarray'>
print(ser_obj.values) # [10 11 12 13 14]
# 获取索引
print(type(ser_obj.index)) # <class 'pandas.core.indexes.range.RangeIndex'>
print(ser_obj.index) # RangeIndex(start=0, stop=5, step=1)
# 预览数据
print(ser_obj.head(3))
#通过索引获取数据
print(ser_obj[0]) # 10
# 索引与数据的对应关系仍保持在数组运算的结果中
print(ser_obj > 12)
print(ser_obj[ser_obj > 12])
<class 'pandas.core.series.Series'>
0 10
1 11
2 12
3 13
4 14
dtype: int32
<class 'numpy.ndarray'>
[10 11 12 13 14]
<class 'pandas.core.indexes.range.RangeIndex'>
RangeIndex(start=0, stop=5, step=1)
0 10
1 11
2 12
dtype: int32
10
0 False
1 False
2 False
3 True
4 True
dtype: bool
3 13
4 14
dtype: int32
通过dict构建Series(注意:字典的key自动作为索引)
year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5}
ser_obj2 = pd.Series(year_data)
print(type(ser_obj2)) # <class 'pandas.core.series.Series'>
print(ser_obj2)
<class 'pandas.core.series.Series'>
2001 17.8
2002 20.1
2003 16.5
dtype: float64
获取数据
print(type(ser_obj2.values)) # <class 'numpy.ndarray'>
print(ser_obj2.values) # [ 17.8 20.1 16.5]
<class 'numpy.ndarray'>
[ 17.8 20.1 16.5]
获取索引
print(type(ser_obj2.index)) # <class 'pandas.core.indexes.numeric.Int64Index'>
print(ser_obj2.index) # Int64Index([2001, 2002, 2003], dtype='int64')
<class 'pandas.core.indexes.numeric.Int64Index'>
Int64Index([2001, 2002, 2003], dtype='int64')
预览数据(head()不加参数则显示全部)
print(ser_obj2.head())
2001 17.8
2002 20.1
2003 16.5
dtype: float64
通过索引获取数据
print(ser_obj2[2001]) # 17.8
17.8
整合代码
# 通过dict构建Series(注意:字典的key自动作为索引)
year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5}
ser_obj2 = pd.Series(year_data)
print(type(ser_obj2)) # <class 'pandas.core.series.Series'>
print(ser_obj2)
# 获取数据
print(type(ser_obj2.values)) # <class 'numpy.ndarray'>
print(ser_obj2.values) # [ 17.8 20.1 16.5]
# 获取索引
print(type(ser_obj2.index)) # <class 'pandas.core.indexes.numeric.Int64Index'>
print(ser_obj2.index) # Int64Index([2001, 2002, 2003], dtype='int64')
# 预览数据(head()不加参数则显示全部)
print(ser_obj2.head())
#通过索引获取数据
print(ser_obj2[2001]) # 17.8
<class 'pandas.core.series.Series'>
2001 17.8
2002 20.1
2003 16.5
dtype: float64
<class 'numpy.ndarray'>
[ 17.8 20.1 16.5]
<class 'pandas.core.indexes.numeric.Int64Index'>
Int64Index([2001, 2002, 2003], dtype='int64')
2001 17.8
2002 20.1
2003 16.5
dtype: float64
17.8
一个Dataframe
就是一张表格,Series
表示的是一维数组,Dataframe
则是一个二维数组,可以类比成一张excel
的spreadsheet
。也可以把 Dataframe
当做一组Series
的集合。
通过ndarray构建DataFrame
import numpy as np
# 通过ndarray构建DataFrame
array = np.random.randn(5,4)
print(array)
df_obj = pd.DataFrame(array)
print(df_obj.head())
[[ 0.7346628 -1.13733651 0.72853785 0.38743511]
[ 0.49549724 3.96998008 1.13567695 -0.21425912]
[ 0.22094222 0.7766603 0.46086182 0.33199643]
[-0.46279419 0.85898771 0.41993259 -0.61997791]
[-0.83296535 1.19450707 -1.45531366 -0.13990243]]
0 1 2 3
0 0.734663 -1.137337 0.728538 0.387435
1 0.495497 3.969980 1.135677 -0.214259
2 0.220942 0.776660 0.460862 0.331996
3 -0.462794 0.858988 0.419933 -0.619978
4 -0.832965 1.194507 -1.455314 -0.139902
通过dict构建DataFrame
dict_data = {'A': 1.,
'B': pd.Timestamp('20180316'),
'C': pd.Series(1, index=list(range(4)),dtype='float32'),
'D': np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["Python","Java","C++","C#"])
}
print(dict_data)
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2.head())
{'A': 1.0, 'B': Timestamp('2018-03-16 00:00:00'), 'C': 0 1.0
1 1.0
2 1.0
3 1.0
dtype: float32, 'D': array([3, 3, 3, 3]), 'E': [Python, Java, C++, C#]
Categories (4, object): [C#, C++, Java, Python]}
A B C D E
0 1.0 2018-03-16 1.0 3 Python
1 1.0 2018-03-16 1.0 3 Java
2 1.0 2018-03-16 1.0 3 C++
3 1.0 2018-03-16 1.0 3 C#
通过列索引获取列数据
print(df_obj2['A'])
print(type(df_obj2['A']))
print(df_obj2.A)
0 1.0
1 1.0
2 1.0
3 1.0
Name: A, dtype: float64
<class 'pandas.core.series.Series'>
0 1.0
1 1.0
2 1.0
3 1.0
Name: A, dtype: float64
通过行索引(.loc)获取行数据
print(df_obj2.loc[0])
print(type(df_obj2.loc[0]))
A 1
B 2018-03-16 00:00:00
C 1
D 3
E Python
Name: 0, dtype: object
<class 'pandas.core.series.Series'>
增加列
df_obj2['F'] = df_obj2['D'] + 4
print(df_obj2.head())
A B C D E F
0 1.0 2018-03-16 1.0 3 Python 7
1 1.0 2018-03-16 1.0 3 Java 7
2 1.0 2018-03-16 1.0 3 C++ 7
3 1.0 2018-03-16 1.0 3 C# 7
删除列
del(df_obj2['F'] )
print(df_obj2.head())
A B C D E
0 1.0 2018-03-16 1.0 3 Python
1 1.0 2018-03-16 1.0 3 Java
2 1.0 2018-03-16 1.0 3 C++
3 1.0 2018-03-16 1.0 3 C#
整合代码
import numpy as np
# 通过ndarray构建DataFrame
array = np.random.randn(5,4)
print(array)
# 通过dict构建DataFrame
df_obj = pd.DataFrame(array)
print(df_obj.head())
dict_data = {'A': 1.,
'B': pd.Timestamp('20180316'),
'C': pd.Series(1, index=list(range(4)),dtype='float32'),
'D': np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["Python","Java","C++","C#"])
}
print(dict_data)
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2.head())
# 通过列索引获取列数据
print(df_obj2['A'])
print(type(df_obj2['A']))
print(df_obj2.A)
# 通过行索引获取行数据
print(df_obj2.loc[0])
print(type(df_obj2.loc[0]))
# 增加列
df_obj2['G'] = df_obj2['D'] + 4
print(df_obj2.head())
# 删除列
del(df_obj2['G'] )
print(df_obj2.head())
[[ 0.23758715 -1.13751056 -0.0863061 -0.71309414]
[ 0.08129935 1.32099551 -0.27057527 0.49270974]
[ 0.96111551 1.08307556 1.5094844 0.96117055]
[-0.31003598 1.33959047 -0.42150857 -1.20605423]
[ 0.12655879 -1.01810288 -1.34025171 0.98758417]]
0 1 2 3
0 0.237587 -1.137511 -0.086306 -0.713094
1 0.081299 1.320996 -0.270575 0.492710
2 0.961116 1.083076 1.509484 0.961171
3 -0.310036 1.339590 -0.421509 -1.206054
4 0.126559 -1.018103 -1.340252 0.987584
{'A': 1.0, 'B': Timestamp('2018-03-16 00:00:00'), 'C': 0 1.0
1 1.0
2 1.0
3 1.0
dtype: float32, 'D': array([3, 3, 3, 3]), 'E': [Python, Java, C++, C#]
Categories (4, object): [C#, C++, Java, Python]}
A B C D E
0 1.0 2018-03-16 1.0 3 Python
1 1.0 2018-03-16 1.0 3 Java
2 1.0 2018-03-16 1.0 3 C++
3 1.0 2018-03-16 1.0 3 C#
0 1.0
1 1.0
2 1.0
3 1.0
Name: A, dtype: float64
<class 'pandas.core.series.Series'>
0 1.0
1 1.0
2 1.0
3 1.0
Name: A, dtype: float64
A 1
B 2018-03-16 00:00:00
C 1
D 3
E Python
Name: 0, dtype: object
<class 'pandas.core.series.Series'>
A B C D E G
0 1.0 2018-03-16 1.0 3 Python 7
1 1.0 2018-03-16 1.0 3 Java 7
2 1.0 2018-03-16 1.0 3 C++ 7
3 1.0 2018-03-16 1.0 3 C# 7
A B C D E
0 1.0 2018-03-16 1.0 3 Python
1 1.0 2018-03-16 1.0 3 Java
2 1.0 2018-03-16 1.0 3 C++
3 1.0 2018-03-16 1.0 3 C#