python笔记:3.2.1.1pandas数据结构

    xiaoxiao2022-07-04  110

    # -*- coding: utf-8 -*- """ Created on Wed May 22 14:12:15 2019 @author: User """ import pandas as pd s1 = pd.Series([100, 78, 59, 63]) print('\n') print(s1) print('\n s1.values:') print(s1.values) print('\n s1.index:') print(s1.index) print('\n 赋值方法改写Series索引:') s1.index=['No.1','No.2','No.3','No.4'] print(s1) print('\n 指定index关键字:') s2 = pd.Series([100, 78, 59, 63], index=['Math','English','Literature','History']) print(s2) print('\n 通过索引访问数据元素:') print(s2[['English','Literature']]) print('\n Series由字典转换而来:') d3={'Name':'zhang san','Gender':'Male','Age':'19','Weight':'76'} s3=pd.Series(d3) print(s3) print('\n Series索引由数组内容代替:') student_attr=['ID','Name','Gender','Age','Grade','Height','Weight'] s4=pd.Series(d3,index=student_attr) print(s4) print('\n 检测缺失值:') print(pd.isnull(s4)) print('\n Series的一个重要功能是自动对齐索引数据:') print(s3+s4) print('\n Series对象本身及索引都具有name属性:') s4.name='Student\'s profile' s4.index.name='Attribute' print(s4) print('\n reindex重新索引:') print(s4.reindex(index=['Name','ID','Age','Gender','Height','Weight','Grade'])) print('\n 新增索引:') s4.index=['b','g','a','c','e','f','d'] print(s4) print('\n reindex:') print(s4.reindex(index=['a','b','c','d','e','f','g','h'])) print('\n reindex并不会改变原索引的实际存储位置,而是返回一个重新索引的视图:') print(s4) print('\n 使用 reindex(index,method=\'***\')的时候,原index必须是单调的:') s4.index=[0,2,3,6,8,9,11] print(s4.reindex(range(10),method='ffill'))

    运行:

    0    100 1     78 2     59 3     63 dtype: int64

     s1.values: [100  78  59  63]

     s1.index: RangeIndex(start=0, stop=4, step=1)

     赋值方法改写Series索引: No.1    100 No.2     78 No.3     59 No.4     63 dtype: int64

     指定index关键字: Math          100 English        78 Literature     59 History        63 dtype: int64

     通过索引访问数据元素: English       78 Literature    59 dtype: int64

     Series由字典转换而来: Name      zhang san Gender         Male Age              19 Weight           76 dtype: object

     Series索引由数组内容代替: ID              NaN Name      zhang san Gender         Male Age              19 Grade           NaN Height          NaN Weight           76 dtype: object

     检测缺失值: ID         True Name      False Gender    False Age       False Grade      True Height     True Weight    False dtype: bool

     Series的一个重要功能是自动对齐索引数据: Age                     1919 Gender              MaleMale Grade                    NaN Height                   NaN ID                       NaN Name      zhang sanzhang san Weight                  7676 dtype: object

     Series对象本身及索引都具有name属性: Attribute ID              NaN Name      zhang san Gender         Male Age              19 Grade           NaN Height          NaN Weight           76 Name: Student's profile, dtype: object

     reindex重新索引: Attribute Name      zhang san ID              NaN Age              19 Gender         Male Height          NaN Weight           76 Grade           NaN Name: Student's profile, dtype: object

     新增索引: b          NaN g    zhang san a         Male c           19 e          NaN f          NaN d           76 Name: Student's profile, dtype: object

     reindex: a         Male b          NaN c           19 d           76 e          NaN f          NaN g    zhang san h          NaN Name: Student's profile, dtype: object

     reindex并不会改变原索引的实际存储位置,而是返回一个重新索引的视图: b          NaN g    zhang san a         Male c           19 e          NaN f          NaN d           76 Name: Student's profile, dtype: object

     使用 reindex(index,method='***')的时候,原index必须是单调的: 0          NaN 1          NaN 2    zhang san 3         Male 4         Male 5         Male 6           19 7           19 8          NaN 9          NaN Name: Student's profile, dtype: object  

    最新回复(0)