Pandas数据结构
2017-09-07 本文已影响90人
b485c88ab697
Pandas数据结构
import pandas as pd
Series
# 通过list构建Series
ser_obj = pd.Series(range(10, 20))
print(type(ser_obj))
<class 'pandas.core.series.Series'>
获取数据/索引
# 获取数据
print(ser_obj.values)
# 获取索引
print(ser_obj.index)
[10 11 12 13 14 15 16 17 18 19]
RangeIndex(start=0, stop=10, step=1)
预览数据
print(ser_obj.head(3))
0 10
1 11
2 12
dtype: int32
print(ser_obj)
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int32
通过索引获取数据
print(ser_obj[0])
print(ser_obj[8])
10
18
索引与数据的对应关系仍保持在数组运算的结果中
print(ser_obj * 2)
print(ser_obj > 15)
0 20
1 22
2 24
3 26
4 28
5 30
6 32
7 34
8 36
9 38
dtype: int32
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 True
8 True
9 True
dtype: bool
通过dict构建Series
year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5}
ser_obj2 = pd.Series(year_data)
print(ser_obj2.head())
print(ser_obj2.index)
2001 17.8
2002 20.1
2003 16.5
dtype: float64
Int64Index([2001, 2002, 2003], dtype='int64')
name属性
ser_obj2.name = 'temp'
ser_obj2.index.name = 'year'
print(ser_obj2.head())
year
2001 17.8
2002 20.1
2003 16.5
Name: temp, dtype: float64
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.59215481 0.6673983 0.74760472 0.25187461]
[-0.46854892 0.00348568 0.25366534 0.11720985]
[ 1.33302115 -0.42943891 -0.06333589 0.30176743]
[-0.81349163 0.62868913 -0.91357273 0.78261241]
[ 0.85686905 0.81765967 0.03702749 -0.32047593]]
0 1 2 3
0 -0.592155 0.667398 0.747605 0.251875
1 -0.468549 0.003486 0.253665 0.117210
2 1.333021 -0.429439 -0.063336 0.301767
3 -0.813492 0.628689 -0.913573 0.782612
4 0.856869 0.817660 0.037027 -0.320476
通过dict构建DataFrame
dict_data = {'A': 1.,
'B': pd.Timestamp('20161217'),
'C': pd.Series(1, index=list(range(4)),dtype='float32'),
'D': np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["Python","Java","C++","C#"]),
'F' : 'ChinaHadoop' }
#print dict_data
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2.head())
A B C D E F
0 1.0 2016-12-17 1.0 3 Python ChinaHadoop
1 1.0 2016-12-17 1.0 3 Java ChinaHadoop
2 1.0 2016-12-17 1.0 3 C++ ChinaHadoop
3 1.0 2016-12-17 1.0 3 C# ChinaHadoop
通过列索引获取列数据
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
增加列
df_obj2['G'] = df_obj2['D'] + 4
print(df_obj2.head())
A B C D E F G
0 1.0 2016-12-17 1.0 3 Python ChinaHadoop 7
1 1.0 2016-12-17 1.0 3 Java ChinaHadoop 7
2 1.0 2016-12-17 1.0 3 C++ ChinaHadoop 7
3 1.0 2016-12-17 1.0 3 C# ChinaHadoop 7
删除列
del(df_obj2['G'] )
print(df_obj2.head())
A B C D E F
0 1.0 2016-12-17 1.0 3 Python ChinaHadoop
1 1.0 2016-12-17 1.0 3 Java ChinaHadoop
2 1.0 2016-12-17 1.0 3 C++ ChinaHadoop
3 1.0 2016-12-17 1.0 3 C# ChinaHadoop
索引对象 Index
print(type(ser_obj.index))
print(type(df_obj2.index))
print(df_obj2.index)
<class 'pandas.core.indexes.range.RangeIndex'>
<class 'pandas.core.indexes.numeric.Int64Index'>
Int64Index([0, 1, 2, 3], dtype='int64')
索引对象不可变
df_obj2.index[0] = 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-9a68e458d492> in <module>()
----> 1 df_obj2.index[0] = 2
C:\Users\weixiao\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
1618
1619 def __setitem__(self, key, value):
-> 1620 raise TypeError("Index does not support mutable operations")
1621
1622 def __getitem__(self, key):
TypeError: Index does not support mutable operations