Data structure of pandas
Data structure import pandas as pd of Pandas
Pandas has two most important and important data structures: Series and DataFrame
Series
A Series is an object similar to an one-dimensional array, consisting of a set of data (various NumPy data types) and a set of corresponding indexes (data labels).
Objects similar to one-dimensional arrays are indexed by data and indexes (index) on the left and data (values) on the right. Build Seriesser_obj = pd.Series (range (10)) through list
Sample code:
# build Seriesser_obj = pd.Series (range (10,20)) print (ser_obj.head (3)) print (ser_obj) print (type (ser_obj)) through list
Running result
0 101 112 12dtype: int640 101 112 123 134 145 156 167 178 189 19dtype: int642. Get data and index ser_obj.index and ser_obj.values
Sample code:
# get data print (ser_obj.values) # get index print (ser_obj.index)
Running result:
[10 11 12 13 14 15 16 17 18 19] RangeIndex (start=0, stop=10, step=1) 3. Get data ser_ OBJ [IDX] through index
Example code:
# get data through index print (ser_obj [0]) print (ser_obj [8])
Running result:
10184. The corresponding relationship between index and data is not affected by the operation result.
Sample code:
# the correspondence between the index and the data is not affected by the operation result print (ser_obj * 2) print (ser_obj > 15)
Running result:
0 201 222 243 264 285 306 327 348 369 38dtype: int640 False1 False2 False3 False4 False5 False6 True7 True8 True9 Truedtype: bool5. Building Series through dict
Sample code:
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)
Running result:
2001 17.82002 20.12003 16.5dtype: float64Int64Index ([2001, 2002, 2003], dtype='int64') name property object name: ser_obj.name
Object index name: ser_obj.index.name
Sample code:
# name attribute ser_obj2.name = 'temp'ser_obj2.index.name =' year'print (ser_obj2.head ())
Running result:
Year2001 17.82002 20.12003 16.5Name: temp, dtype: float64DataFrame
DataFrame is a tabular data structure that contains an ordered set of columns, each of which can be a different type of value. DataFrame has both row and column indexes, which can be thought of as a dictionary of Series (sharing the same index), and the data is stored in a two-dimensional structure.
Similar multidimensional array / table data (e.g., excel, data.frame in R) each column data can be a different type of index, including column index and row index 1. Building DataFrame through ndarray
Sample code:
Import numpy as np# builds DataFramearray = np.random.randn (5,4) print (array) df_obj = pd.DataFrame (array) print (df_obj.head ()) through ndarray
Running result:
[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]] 01 2 30 0.835006-1.492901-0.531201-0 . 1131391 0.646298-0.367799 0.080111 0.6008052-1.234585 0.334097-0.587782-0.7361063-1.476514 0.994002 0.210020-0.9051574 0.566694 1.382383-0.490990 1.9448462. Building DataFrame through dict
Sample code:
# build DataFramedict_data = {'Aids: 1,' breadth: pd.Timestamp ('20170426'), 'index: pd.Series (1, index = list (range (4)), dtype =' float32'), 'dating: np.array ([3] * 4, dtype =' int32'), 'eBay: ["Python", "Java", "C++", "C"] 'Flying: 'ITCast'} # print dict_datadf_obj2 = pd.DataFrame (dict_data) print (df_obj2)
Running result:
A B C D E F0 1 2017-04-26 1.03 Python ITCast1 1 2017-04-26 1.0 3 Java ITCast2 1 2017-04-26 1.0 3 C++ ITCast3 1 2017-04-26 1.0 3 C ITCast3. Get column data through column index (Series type) df_ obj [col _ idx] or df_obj.col_idx
Sample code:
Print (df_obj2 ['A']) print (type (df_obj2 ['A'])) print (df_obj2.A)
Running result:
0 1.01 1.02 1.03 1.0Name: A, dtype: float640 1.01 1.02 1.03 1.0Name: A, dtype: float644. Add column data df_ OBJ [new _ col_idx] = data
Add key-value to dict similar to Python
Sample code:
Df_obj2 ['G'] = df_obj2 ['D'] + 4print (df_obj2.head ())
Running result:
A B C D E F G0 1.0 2017-01-02 1.0 3 Python ITCast 71 1.0 2017-01-02 1.0 3 Java ITCast 72 1.0 2017-01-02 1.0 3 C++ ITCast 73 1.0 2017-01-02 1.0 3 C ITCast 75. Delete the column del df_ obj [col _ idx]
Sample code:
Del (df_obj2 ['G']) print (df_obj2.head ())
Running result:
A B C D E F0 1.0 2017-01-02 1.0 3 Python ITCast1 1.0 2017-01-02 1.0 3 Java ITCast2 1.0 2017-01-02 1.0 3 C++ ITCast3 1.0 2017-01-02 1.0 3 C ITCast