您好,登錄后才能下訂單哦!
import pandas as pd
Pandas有兩個最主要也是最重要的數據結構:Series和DataFrame
Series
Series是一種類似于一維數組的對象,由一組數據(各種NumPy數據類型)以及一組與之對應的索引(數據標簽)組成。
ser_obj = pd.Series(range(10))
示例代碼:
# 通過list構建Series
ser_obj = pd.Series(range(10, 20))
print(ser_obj.head(3))
print(ser_obj)
print(type(ser_obj))
運行結果
0 10
1 11
2 12
dtype: int64
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int64
<class 'pandas.core.series.Series'>
ser_obj.index 和 ser_obj.values
示例代碼:
# 獲取數據
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)
ser_obj[idx]
實例代碼:
# 通過索引獲取數據
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: int64
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 True
8 True
9 True
dtype: bool
示例代碼:
year_data = {2001: 17.8, 2002: 20.1, 2003: 16.3}
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')
對象名:ser_obj.name
對象索引名:ser_obj.index.name
示例代碼:
# 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
DataFrame是一個表格型的數據結構,它含有一組有序的列,每列可以是不同類型的值。DataFrame既有行索引也有列索引,它可以被看作是由Series組成的字典(共用同一個索引),數據是以二維結構存放的。
示例代碼:
import numpy as np
# 通過ndarray構建DataFrame
array = np.random.randn(5, 4)
print(array)
df_obj = pd.DataFrame(array)
print(df_obj.head())
運行結果:
[[ 0.83500594 -1.49290138 -0.53120106 -0.11313932]
[ 0.64629762 -0.36779941 0.08011084 0.60080495]
[-1.23458522 0.33409674 -0.58778195 -0.73610573]
[-1.47651414 0.99400187 0.21001995 -0.90515656]
[ 0.56669419 1.38238348 -0.49099007 1.94484598]]
0 1 2 3
0 0.835006 -1.492901 -0.531201 -0.113139
1 0.646298 -0.367799 0.080111 0.600805
2 -1.234585 0.334097 -0.587782 -0.736106
3 -1.476514 0.994002 0.210020 -0.905157
4 0.566694 1.382383 -0.490990 1.944846
示例代碼:
# 通過dict構建DataFrame
dict_data = {'A': 1,
'B': pd.Timestamp('20170426'),
'C': pd.Series(1, index = list(range(4)), dtype = 'float32'),
'D': np.array([3] * 4, dtype = 'int32'),
'E': ["Python", "Java", "C++", "C"],
'F': 'ITCast' }
#print dict_data
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2)
運行結果:
A B C D E F
0 1 2017-04-26 1.0 3 Python ITCast
1 1 2017-04-26 1.0 3 Java ITCast
2 1 2017-04-26 1.0 3 C++ ITCast
3 1 2017-04-26 1.0 3 C ITCast
df_obj[col_idx]或df_obj.col_idx
示例代碼:
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_obj[new_col_idx] = data
類似Python的dict添加key-value
示例代碼:
df_obj2['G'] = df_obj2['D'] + 4
print(df_obj2.head())
運行結果:
A B C D E F G
0 1.0 2017-01-02 1.0 3 Python ITCast 7
1 1.0 2017-01-02 1.0 3 Java ITCast 7
2 1.0 2017-01-02 1.0 3 C++ ITCast 7
3 1.0 2017-01-02 1.0 3 C ITCast 7
del df_obj[col_idx]
示例代碼:
del(df_obj2['G'])
print(df_obj2.head())
運行結果:
A B C D E F
0 1.0 2017-01-02 1.0 3 Python ITCast
1 1.0 2017-01-02 1.0 3 Java ITCast
2 1.0 2017-01-02 1.0 3 C++ ITCast
3 1.0 2017-01-02 1.0 3 C ITCast
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。