中期_1.Python的Numpy,Pandas库使用_ch06--numpy初始

1.pandas

  • pandas是基于Numpy的一种工具,该工具是为了解决数据分析任务而创建的,
  • Pandas是python的一个数据分析包,使用时就需要分别引入高级结构和工具模块,工具模块一般别名为pd

Series

  • 通过 from pandas import Series 导入
  • Series对象组成结构
  • Series是由一组数据以及一组与之相关的数据标签(索引)组成
Series(random.randint(1000,4000,size=10),index=list(range(10)))

https://blog.csdn.net/kingov/article/details/79513322

  • 根据index获取dict中的value,利用split()切分获取key,没有匹配到的key 显示 NaN
Series(data,index='AirCondition,Computer,SmartTV,PhoneTest'.split(',')) #索引肯定是从列表中取,split()切割成列表

demo01 2021-05-07

对Series类型数据进行探查

data = {
    'test01':10,
    'test02':101,
    'test03':1001
}

sf = Series(data,index='test01 test02 test04'.split())
  • 找缺失数据

    import pandas as pd
    pd.isnull(sf)
    -------------------------
    test01    False
    test02    False
    test04     True
    dtype: bool
    
  • 找有数值数据,过滤掉缺失值

    pd.notnull(sf)
    ----------------------
    test01     True
    test02     True
    test04    False
    dtype: bool
    
  • 找出为空的数据,并打印

    sf[pd.isnull(sf)]
    -----------------
    test04   NaN
    dtype: float64
    
  • 找出不为空的数据,并打印

    sf[pd.notnull(sf)]
    -------------------
    test01     10.0
    test02    101.0
    dtype: float64
    
  • 可以进行间的的运算

    sf.name='测试数据' #指定数据是干嘛的
    sf.index.name='测试类别' #指定index
    sf.reset_index() # 转换dataframe对象直接将name转换为列名
    

demo02 2021-05-08

  • DataFrame :表格型的数据结构包含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型等),DataFrame即有行索引也有列索引,可以被看做是由Series组成的字典。
  • Series :一个一维的数组对象,(类似Numpy的一维array) 它除了包含一组数据还包含一组索引,所以可以把它理解为一组带索引的数组。

参考:https://blog.csdn.net/snow__wei/article/details/100666418

https://zhuanlan.zhihu.com/p/131553804

import pandas as pd
from numpy import random
#随机产生形状(shape)为(3,3),范围的(1000,2000)里的整数
data = random.randint(1000,2000,size(3,3))
df = pd.DataFrame(data=data,columns=['A','B','C'],index=[0,1,2])
df
-------------------------
   A       B      C
0	1663	1981	1587
1	1001	1367	1443
2	1322	1443	1929

Pandas技巧之Series转换至DataFrame:https://www.jianshu.com/p/7618ab9ddc11

posted @ 2021-07-12 09:17  小石小石摩西摩西  阅读(47)  评论(0编辑  收藏  举报