Python3 Quick Start (xiii) - Pandas data structure

Python3 Quick Start (xiii) - Pandas data structure

A, Pandas data structure Introduction

Pandas There are three main data structures, Series, DataFrame, Panel.
Series with the tag is a one-dimensional array, and can hold any data type (integer, string, floating point, Python objects, etc.), collectively referred to as the axis labels index (index).
DataFrame is two-dimensional data structure with the tag having index (tag line) and Columns (column labels). If the index is transmitted or columns, will be generated for DataFrame index or columns.
Panel D is a data structure defined by the items, major_axis, minor_axis. items (entry), i.e. the axis 0, each entry corresponding to a DataFrame; major_axis (spindle), i.e. the shaft 1, each DataFrame in index (rows); minor_axis (countershaft), i.e. the shaft 2, each of DataFrame columns (columns).

Two, Series

1, Series Introduction

Series is capable of storing data of any type (integer, string, floating point, Python object, etc.) of the one-dimensional array of marks, referred to as axis labels index (index).
is a series of one-dimensional data structure, each element having an index, where the index may be a number or string. Series structure Name:
Python3 Quick Start (xiii) - Pandas data structure

2, Series object constructor

Series constructor as follows:
pandas.Series( data, index, dtype, copy)
Data: Construction Series data may be ndarray, list, dict, constants.
index: the index value must be unique hash is the same as the length of the data. If the index is not transmitted, default np.arange (n).
dtype: data types, and if not, we will infer data types.
copy: whether to copy the data, the default is false.
(1) Create an empty Series

import pandas as pd

if __name__ == "__main__":
    s = pd.Series()
    print(s)

# output:
# Series([], dtype: float64)

(2) create Series ndarray
used ndarray as the data, the index must have the same transfer length ndarray. If the index value is not passed, then the default indexes are Range (n), where n is the length of the array, i.e., [0,1,2,3 ... range (len (array )) -. 1] - 1].

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = np.array(['a', 1, 2, 4, 6])
    s = pd.Series(data,index=[101, 102, 103, 'hello', 'world'])
    print(s)

# output:
# 101      a
# 102      1
# 103      2
# hello    4
# world    6
# dtype: object

When the index does not pass any default distribution from 0 to len (data) index -1.

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = np.array(['a', 1, 2, 4, 6])
    s = pd.Series(data)
    print(s)

# output:
# 0    a
# 1    1
# 2    2
# 3    4
# 4    6
# dtype: object

(3) Use a dictionary to create Series
use a dictionary (dict) as the data, if you do not specify the index, press the sort order to obtain the dictionary key to construct the index. If you pass the index, the index value corresponding to the label data will be removed.

import pandas as pd

if __name__ == "__main__":
    data = {'a': 1, 2: 'hello', 'b': 'hello world'}
    s = pd.Series(data)
    print(s)

# output:
# a              1
# 2          hello
# b    hello world
# dtype: object

When transmitting the index, the sequence remains unchanged, missing elements using NaN (Not a Number) is filled.

import pandas as pd

if __name__ == "__main__":
    data = {'a': 1, 2: 'hello', 'b': 'hello world', "hello": "world"}
    s = pd.Series(data, index=['a', 'b', "hello", 'd'])
    print(s)

# output:
# a                  1
# b        hello world
# hello          world
# d                NaN
# dtype: object

(4) to create a scalar Series
using a scalar value as the data, the index must be provided, the scalar value is repeated to match the length of the index.

import pandas as pd

if __name__ == "__main__":
    s = pd.Series(100, index=[1, 2, 3])
    print(s)

# output:
# 1    100
# 2    100
# 3    100
# dtype: int64

(5) List, Series tuple created
using List, as the index tuple data transfer must List, tuple having the same length. If the index value is not passed, then the default indexes are Range (n), where n is the length of the list, i.e., [0,1,2,3 ... range (len (list )) -. 1] - 1].

import pandas as pd

if __name__ == "__main__":
    s = pd.Series([1, 2, 3, "hello"])
    print(s)

# output:
# 0        1
# 1        2
# 2        3
# 3    hello
# dtype: object

3, Series data access

Series data can be used to access an ordered sequence.

import pandas as pd

if __name__ == "__main__":
    s = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])
    print(s[0])
    print(s[-1])
    print(s[-3:])

# output:
# 1
# 5
# c    3
# d    4
# e    5
# dtype: int64

Series like a dictionary of fixed size, and can be obtained by setting the value of an index tab, using the index value to retrieve a single element tag, the tag list using the index value to retrieve a plurality of elements. If a label is not included in the index, an exception occurs.

import pandas as pd

if __name__ == "__main__":
    s = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])
    s['a'] = 101
    print(s['a'])
    print(s[0])
    print(s[['a', 'b', 'e']])

# output:
# 101
# 101
# a    101
# b      2
# e      5
# dtype: int64

4, Series Properties

Series object properties and methods as follows:
Series.axes: Returns the row axes tag list
Series.dtype: Returns the object's data type
Series.empty: if the object is empty, return True
Series.ndim: Returns the number of dimensions of the underlying data, default . 1
Series.size: returns the number of elements in the base data
Series.values: returns the object as ndarray
Series.head (): returns the first n rows
Series.tail (): returns the row n

import pandas as pd

if __name__ == "__main__":
    s = pd.Series(["Bauer", 30, 90], index=['Name', 'Age', 'Score'])
    print("Series=================")
    print(s)
    print("axes===================")
    print(s.axes)
    print("dtype==================")
    print(s.dtype)
    print("empty==================")
    print(s.empty)
    print("ndim===================")
    print(s.ndim)
    print("size===================")
    print(s.size)
    print("values=================")
    print(s.values)
    print("head()=================")
    print(s.head(2))
    print("tail()=================")
    print(s.tail(2))

# output:
# Series=================
# Name     Bauer
# Age         30
# Score       90
# dtype: object
# axes===================
# [Index(['Name', 'Age', 'Score'], dtype='object')]
# dtype==================
# object
# empty==================
# False
# ndim===================
# 1
# size===================
# 3
# values=================
# ['Bauer' 30 90]
# head()=================
# Name    Bauer
# Age        30
# dtype: object
# tail()=================
# Age      30
# Score    90
# dtype: object

三, DataFrame

1, DataFrame Profile

Data frame (DataFrame) is two-dimensional tabular data structures, i.e., data in tabular form are arranged in rows and columns, a Series DataFrame container.
Name DataFrame structure is as follows:
Python3 Quick Start (xiii) - Pandas data structure

2, DataFrame features

Data frame (DataFrame) the features are as follows:
(1) the underlying data columns are of different types
(2) of variable size
(3) labeled axes (rows and columns)
(4) can perform arithmetic operations on the rows and columns

3, DataFrame object constructor

pandas.DataFrame( data, index, columns, dtype, copy)
data: Construction DataFrame data may be ndarray, series, map, lists, dict, constant and other DataFrame.
index: an index tab line, if the index value is not passed, the default is the index np.arrange (n).
columns: column index tab, if not passed column index value, the default column index is np.arange (n).
dtype: the data type of each column.
copy: If the default value is False, the command (or any of its) for copying data.
(1) create an empty DataFrame

import pandas as pd

if __name__ == "__main__":
    df = pd.DataFrame()
    print(df)

# output:
# Empty DataFrame
# Columns: []
# Index: []

(2) use the list to create DataFrame
used as a single list or a nested list data created DataFrame, or if not specified index Columns, default range (len (list)) as an index, for a single list, the default columns = [0], for the nested list, the default range of the length of the inner columns list.

import pandas as pd

if __name__ == "__main__":
    data = [1, 2, 3, 4, 5]
    df = pd.DataFrame(data)
    print(df)
    df = pd.DataFrame(data, index=['a', 'b', 'c', 'h', 'e'], columns=['A'])
    print(df)

# output:
#    0
# 0  1
# 1  2
# 2  3
# 3  4
# 4  5
#    A
# a  1
# b  2
# c  3
# h  4
# e  5

When the index is specified or columns, the length of the index list must match the length, the length of the inner columns must match the length of the list of the list, otherwise an error.

import pandas as pd

if __name__ == "__main__":
    data = [['Alex', 25], ['Bob', 26], ['Bauer', 24]]
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    print(df)
    df = pd.DataFrame(data, columns=['Name', 'Age'], dtype=float)
    print(df)

# output:
#     Name  Age
# 0   Alex   25
# 1    Bob   26
# 2  Bauer   24
#     Name   Age
# 0   Alex  25.0
# 1    Bob  26.0
# 2  Bauer  24.0

(3) use of the list and the dictionary creating ndarray DataFrame
use ndarray, list composition data as dictionary created DataFrame, all ndarray, list must have the same length. If the index is transmitted, the index must be equal to the length of ndarray, list length, the set of columns for a key component of the dictionary. If there is no transmission index, by default, index will range (n), where n is the length of the list or ndarray.

import pandas as pd

if __name__ == "__main__":
    data = {'Name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'Age': [28, 34, 29, 42]}
    df = pd.DataFrame(data)
    print(df)
    df = pd.DataFrame(data, index=['rank1', 'rank2', 'rank3', 'rank4'])
    print(df)

# output:
#     Name  Age
# 0    Tom   28
# 1   Jack   34
# 2  Steve   29
# 3  Ricky   42
#         Name  Age
# rank1    Tom   28
# rank2   Jack   34
# rank3  Steve   29
# rank4  Ricky   42

(4) Create a list using the dictionary DataFrame
used as the data dictionary list created DataFrame, default range (len (list)) as an index, a set of dictionary key as columns, if no corresponding dictionary key-value pair, the value used to fill NaN. When specifying columns, if the columns used as a set of elements other than element dictionary key columns, use NaN filled, and extracts the specified data source columns corresponding dictionary pairs.

import pandas as pd

if __name__ == "__main__":
    data = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
    df = pd.DataFrame(data)
    print(df)
    df = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', 'c', 'A', 'B'])
    print(df)

# output:
#    a   b     c
# 0  1   2   NaN
# 1  5  10  20.0
#         a   b     c   A   B
# first   1   2   NaN NaN NaN
# second  5  10  20.0 NaN NaN

(5) to create the Series DataFrame dictionary
when creating DataFrame used as a data dictionary Series, DataFrame index is the index obtained for all Series and set, as set dictionary key columns.

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
    df = pd.DataFrame(data, columns=['one', 'two'])
    print(df)
    df = pd.DataFrame(data, columns=['one', 'two', 'three'])
    print(df)
    df = pd.DataFrame(data, index=['a', 'b', 'c', 'A'], columns=['one', 'two', 'three'])
    print(df)

# output:
#    one  two
# a  1.0    1
# b  2.0    2
# c  3.0    3
# d  NaN    4
#    one  two three
# a  1.0    1   NaN
# b  2.0    2   NaN
# c  3.0    3   NaN
# d  NaN    4   NaN
#    one  two three
# a  1.0  1.0   NaN
# b  2.0  2.0   NaN
# c  3.0  3.0   NaN
# A  NaN  NaN   NaN

4, DataFrame column operations

The column may be selected by the dictionary key, acquired in a data DataFrame.

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
    df = pd.DataFrame(data, columns=['one', 'two'])
    print(df)
    print(df['one'])

# output:
#    one  two
# a  1.0    1
# b  2.0    2
# c  3.0    3
# d  NaN    4
# a    1.0
# b    2.0
# c    3.0
# d    NaN
# Name: one, dtype: float64

By increasing the value corresponding to the key and Series DataFrame, may be added as a DataFrame.

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
    df = pd.DataFrame(data, columns=['one', 'two'])
    print(df)
    df['three'] = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
    print(df)
    df['four'] = df['one'] + df['three']
    print(df)

# output:
#    one  two
# a  1.0    1
# b  2.0    2
# c  3.0    3
# d  NaN    4
#    one  two  three
# a  1.0    1   10.0
# b  2.0    2   20.0
# c  3.0    3   30.0
# d  NaN    4    NaN
#    one  two  three  four
# a  1.0    1   10.0  11.0
# b  2.0    2   20.0  22.0
# c  3.0    3   30.0  33.0
# d  NaN    4    NaN   NaN

DataFrame can delete columns by del.

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
            'three': pd.Series([10, 20, 30], index=['a', 'b', 'c'])}
    df = pd.DataFrame(data, columns=['one', 'two', 'three'])
    print(df)
    del(df['two'])
    print(df)

# output:
#  one  two  three
# a  1.0    1   10.0
# b  2.0    2   20.0
# c  3.0    3   30.0
# d  NaN    4    NaN
#    one  three
# a  1.0   10.0
# b  2.0   20.0
# c  3.0   30.0
# d  NaN    NaN

5, DataFrame row selection

DataFrame row selection by transferring the row labels to loc function to select the line, may be by passing integer positions to iLoc () function to select the rows to return Series, name Series is retrieved tag, Series of index is Columns DataFrame of .

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
            'three': pd.Series([10, 20, 30], index=['a', 'b', 'c'])}
    df = pd.DataFrame(data, columns=['one', 'two', 'three'])
    print(df.loc['a'])
    print(df.iloc[0])

# output:
# one       1.0
# two       1.0
# three    10.0
# Name: a, dtype: float64
# one       1.0
# two       1.0
# three    10.0
# Name: a, dtype: float64

DataFrame by using a multi-row selection: DataFrame operator to perform row slicing operation, select multiple rows.

import pandas as pd

if __name__ == "__main__":
    data = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
            'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
            'three': pd.Series([10, 20, 30], index=['a', 'b', 'c'])}
    df = pd.DataFrame(data, columns=['one', 'two', 'three'])
    print(df)
    print(df[2:4])

# output:
#    one  two  three
# a  1.0    1   10.0
# b  2.0    2   20.0
# c  3.0    3   30.0
# d  NaN    4    NaN
#    one  two  three
# c  3.0    3   30.0
# d  NaN    4    NaN

DataFrame row additional function is achieved by append.

import pandas as pd

if __name__ == "__main__":
    df = pd.DataFrame([["Bauer", 20], ["Jack", 30]], index=["rank1", "rank2"], columns=['Name', 'Age'])
    df2 = pd.DataFrame([["Alex", 18], ["Bob", 28]], index=["rank3", "rank3"], columns=['Name', 'Age'])
    df = df.append(df2)
    print(df)

# output:
#         Name  Age
# rank1  Bauer   20
# rank2   Jack   30
# rank3   Alex   18
# rank3    Bob   28

DataFrame delete rows by passing the index tab to the drop function to delete the line, if the label is repeated, it will delete lines.

import pandas as pd

if __name__ == "__main__":
    df = pd.DataFrame([["Bauer", 20], ["Jack", 30]], index=["rank1", "rank2"], columns=['Name', 'Age'])
    df2 = pd.DataFrame([["Alex", 18], ["Bob", 28]], index=["rank3", "rank3"], columns=['Name', 'Age'])
    df = df.append(df2)
    print(df)
    df = df.drop("rank2")
    print(df)

# output:
#         Name  Age
# rank1  Bauer   20
# rank2   Jack   30
# rank3   Alex   18
# rank3    Bob   28
#         Name  Age
# rank1  Bauer   20
# rank3   Alex   18
# rank3    Bob   28

6, DataFrame property

DataFrame object properties and methods as follows:
DataFrame.T: transpose rows and columns
DataFrame.axes: Returns a column, row and column labels shaft axis labels as the only member.
DataFrame.dtypes: Returns the object's data type
DataFrame.empty: If NDFrame completely empty, returns True
DataFrame.ndim: Back axle / array dimension size
DataFrame.shape: Returns the tuple DataFrame dimensions
DataFrame.size: Returns the DataFrame the number of elements
DataFrame.values: returns the object as ndarray
DataFrame.head (): returns the first n rows
DataFrame.tail (): returns the row n

import pandas as pd

if __name__ == "__main__":
    df = pd.DataFrame([["Bauer", 30, 90], ['Jack', 32, 98], ['Bob', 28, 78]], columns=['Name', 'Age', 'Score'])
    print("DataFrame=================")
    print(df)
    print("T======================")
    print(df.T)
    print("axes===================")
    print(df.axes)
    print("dtypes==================")
    print(df.dtypes)
    print("empty==================")
    print(df.empty)
    print("ndim===================")
    print(df.ndim)
    print("shape==================")
    print(df.shape)
    print("size===================")
    print(df.size)
    print("values=================")
    print(df.values)
    print("head()=================")
    print(df.head(2))
    print("tail()=================")
    print(df.tail(2))

# output:
# DataFrame=================
#     Name  Age  Score
# 0  Bauer   30     90
# 1   Jack   32     98
# 2    Bob   28     78
# T======================
#            0     1    2
# Name   Bauer  Jack  Bob
# Age       30    32   28
# Score     90    98   78
# axes===================
# [RangeIndex(start=0, stop=3, step=1), Index(['Name', 'Age', 'Score'], dtype='object')]
# dtypes==================
# Name     object
# Age       int64
# Score     int64
# dtype: object
# empty==================
# False
# ndim===================
# 2
# shape==================
# (3, 3)
# size===================
# 9
# values=================
# [['Bauer' 30 90]
#  ['Jack' 32 98]
#  ['Bob' 28 78]]
# head()=================
#     Name  Age  Score
# 0  Bauer   30     90
# 1   Jack   32     98
# tail()=================
#    Name  Age  Score
# 1  Jack   32     98
# 2   Bob   28     78

Four, Panel

1, Panel Introduction

Panel three-dimensional data structure, the container is DataFrame, Panel three axes as follows:
items - Axis 0, each data frame corresponds to the item contained within (DataFrame).
major_axis - axis 1, it is an index for each data frame (DataFrame) (row).
minor_axis - axis 2, each data frame (DataFrame) column.

2, Panel objects built

pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
data: Panel Construction of data, take various forms, such as: ndarray, series, map, lists , dict, constant , and another data frame (DataFrame).
items: Axis = 0
major_axis: Axis =. 1
minor_axis: Axis = 2
DTYPE: the data type of each column
copy: copy data, default - to false
(. 1) creates an empty Panel

import pandas as pd

if __name__ == "__main__":
    p = pd.Panel()
    print(p)

# output:
# class 'pandas.core.panel.Panel'>
# Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)
# Items axis: None
# Major_axis axis: None
# Minor_axis axis: None

(2) the use of 3D ndarray create Panel

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = np.random.rand(2, 4, 5)
    p = pd.Panel(data, items=["item1", "item2"], major_axis=[1, 2, 3, 4], minor_axis=['a', 'b', 'c', 'd', 'e'])
    print(p)
    print("item1")
    print(p["item1"])
    print(p.major_xs(2))
    print(p.minor_xs('b'))

# output:
# <class 'pandas.core.panel.Panel'>
# Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
# Items axis: item1 to item2
# Major_axis axis: 1 to 4
# Minor_axis axis: a to e
# item1
#           a         b         c         d         e
# 1  0.185626  0.976123  0.566263  0.273208  0.675442
# 2  0.209664  0.205190  0.217200  0.158447  0.400683
# 3  0.499591  0.963321  0.759330  0.089930  0.362824
# 4  0.723158  0.585642  0.629246  0.886086  0.493039
#       item1     item2
# a  0.209664  0.592154
# b  0.205190  0.661562
# c  0.217200  0.743716
# d  0.158447  0.055882
# e  0.400683  0.245760
#       item1     item2
# 1  0.976123  0.630320
# 2  0.205190  0.661562
# 3  0.963321  0.741791
# 4  0.585642  0.729366

(3) use DataFrame dictionary creation Panel

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = {'Table1': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']),
            'Table2': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']
                                   )
            }
    p = pd.Panel(data)
    print(p)

# output:
# <class 'pandas.core.panel.Panel'>
# Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
# Items axis: Table1 to Table2
# Major_axis axis: rank1 to rank4
# Minor_axis axis: Name to Score

3, Panel Data Index

Use Items Access Panel may obtain the appropriate DataFrame.

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = {'Table1': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']),
            'Table2': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']
                                   )
            }
    p = pd.Panel(data)
    print(p['Table1'])

# output:
#            Name       Age     Score
# rank1 -1.240644 -0.820041  1.656150
# rank2  1.830655 -0.258068 -0.728560
# rank3  1.268695  1.259693 -1.005151
# rank4 -0.139876  0.611589  2.343394

Use panel.major_axis (key) function to access Panel data needs to be passed as the value Major_axis key, returns DataFrame, DataFrame the index is Minor_axis, columns of Items.

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = {'Table1': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']),
            'Table2': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']
                                   )
            }
    p = pd.Panel(data)
    print(p.major_xs('rank2'))

# output:
#          Table1    Table2
# Name   1.664996  0.326820
# Age    0.952639  0.686095
# Score -0.473985 -0.343404

Use panel.minor_axis (key) function to access Panel data needs to be passed as the value Minor_axis key, returns DataFrame, DataFrame the index is Major_axis, columns of Items.

import pandas as pd
import numpy as np

if __name__ == "__main__":
    data = {'Table1': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']),
            'Table2': pd.DataFrame(np.random.randn(4, 3),
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']
                                   )
            }
    p = pd.Panel(data)
    print(p.minor_xs('Name'))

# output:
#          Table1    Table2
# rank1 -1.314702 -0.198485
# rank2  0.055324  0.295646
# rank3 -0.352192 -0.523549
# rank4 -4.002903 -0.577389

4, Panel Properties

Panel object properties and methods as follows:
Panel.T: transpose rows and columns
Panel.axes: Returns a column, row and column labels shaft axis labels as the only member.
Panel.dtypes: Returns the object's data type
Panel.empty: If NDFrame completely empty, returns True
Panel.ndim: Back axle / array dimension size
Panel.shape: Returns DataFrame dimension tuple
Panel.size: Returns the DataFrame the number of elements
Panel.values: the object is returned as ndarray

import pandas as pd
import numpy as np

if __name__ == "__main__":
    array1 = np.random.randn(4, 3)
    array2 = np.random.randn(4, 3)
    data = {'Table1': pd.DataFrame(array1,
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']),
            'Table2': pd.DataFrame(array2,
                                   index=['rank1', 'rank2', 'rank3', 'rank4'],
                                   columns=['Name', 'Age', 'Score']
                                   )
            }
    p = pd.Panel(data)
    print("panel=================")
    print(p)
    print("axes===================")
    print(p.axes)
    print("dtypes==================")
    print(p.dtypes)
    print("empty==================")
    print(p.empty)
    print("ndim===================")
    print(p.ndim)
    print("shape==================")
    print(p.shape)
    print("size===================")
    print(p.size)
    print("values=================")
    print(p.values)
    print(p["Table1"])
    print(array1)

# output:
# panel=================
# <class 'pandas.core.panel.Panel'>
# Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
# Items axis: Table1 to Table2
# Major_axis axis: rank1 to rank4
# Minor_axis axis: Name to Score
# axes===================
# [Index(['Table1', 'Table2'], dtype='object'), Index(['rank1', 'rank2', 'rank3', 'rank4'], dtype='object'), Index(['Name', 'Age', 'Score'], dtype='object')]
# dtypes==================
# Table1    float64
# Table2    float64
# dtype: object
# empty==================
# False
# ndim===================
# 3
# shape==================
# (2, 4, 3)
# size===================
# 24
# values=================
# [[[ 0.22914664 -0.88176603  0.48050365]
#   [-0.15099586  0.23380446  0.20165317]
#   [-0.13652604  1.08191771  0.60361811]
#   [-0.81742392 -0.09018878  1.62892609]]
# 
#  [[-0.72965894  0.58207009  0.15309812]
#   [ 0.06467707  1.13494668 -0.19074456]
#   [-0.53869056  1.28244925 -0.01832724]
#   [-0.26831567  0.65912009  0.38607594]]]
#            Name       Age     Score
# rank1  0.229147 -0.881766  0.480504
# rank2 -0.150996  0.233804  0.201653
# rank3 -0.136526  1.081918  0.603618
# rank4 -0.817424 -0.090189  1.628926
# [[ 0.22914664 -0.88176603  0.48050365]
#  [-0.15099586  0.23380446  0.20165317]
#  [-0.13652604  1.08191771  0.60361811]
#  [-0.81742392 -0.09018878  1.62892609]]

Guess you like

Origin blog.51cto.com/9291927/2428237