最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

第1关:Series数据选择

IT圈 admin 3浏览 0评论

第1关:Series数据选择

任务描述

本关任务:根据相关知识,得到目标Series对象,具体要求请查看编程要求。

相关知识

Series对象与一维Numpy数组和标准Python字典在许多方面都一样。所以,Series对象的数据索引和选择模式与以上两种数据结构类似。

Series数据选择方法

Python中的字典一样,Series对象提供了键值对的映射。

 
  1. In: import pandas as pd
  2. In: data = pd.Series([0.25, 0.5, 0.75, 1.0], index=['a', 'b', 'c', 'd'])
  3. In: data["b"]
  4. Out: 0.5

还可以用Python字典的表达式和方法来检测键/索引和值,也可以像字典一样来修改Series对象的值。

 
  1. In: "a" in data
  2. Out: True
  3. In: data.keys()
  4. Out: Index(['a', 'b', 'c', 'd'], dtype='object')
  5. In: list(data.items())
  6. Out: [('a', 0.25), ('b', 0.5), ('c', 0.75), ('d', 1.0)]
  7. In: data["b"] = 0.05 # 也可以通过此方法来扩展Series
  8. In: data
  9. Out: a 0.25
  10. b 0.05
  11. c 0.75
  12. d 1.00
  13. dtype: float64

Series对象的可变性是一个非常方便的特性:Pandas在底层已经为可能发生的内存布局和数据复制自动决策,用户不需要担心这些问题。

将Series看作一堆数组

Series对象还具备和Numpy数组一样的数组数据选择功能,包括索引、掩码、花哨索引等操作,具体示例如下所示:

  • 将显示索引作为切片:

    注意:显示索引切片结果包含最后一个索引,也就是能取到“c”的值。

     
    1. In: data['a':'c']
    2. Out: a 0.25
    3. b 0.50
    4. c 0.75
    5. dtype: float64
  • 将隐式整数索引作为切片:

    注意:隐式索引切片结果不包含最后一个索引。

     
    1. In: data[0:2]
    2. Out: a 0.25
    3. b 0.50
    4. dtype: float64
  • 掩码:

     
    1. In: data[(data > 0.3) & (data < 0.8)]
    2. Out: b 0.50
    3. c 0.75
    4. dtype: float64
  • 花哨的索引:

     
    1. In: data[["a","e"]]
    2. Out: a 0.25
    3. e 1.25
    4. dtype: float64

索引器:loc和iloc

这些切片和取值操作非常混乱,假如Series对象索引序列为整数时,data[2]不会取第三行,而是取索引序列为2的那一行,也就是说会优先使用显示索引,而data[1:3]这样的切片操作会优先使用隐式索引。

 
  1. In: data = pd.Series(['a', 'b', 'c'], index=[1, 3, 5])
  2. In: data[1]
  3. Out: "a"
  4. In: data[0:2]
  5. Out: 1 a
  6. 3 b
  7. dtype: object

正是应为这些整数索引很容易造成混淆,所以Pandas提供了一些 索引器(indexer) 属性来作为取值的方法。它们不是Series对象的函数方法,而是暴露切片接口的属性。

  • loc属性:表示取值和切片都是显式的;
     
    1. In: data.loc[1]
    2. Out: "a"
    3. In: data.loc[1:3]
    4. Out: 1 a
    5. 3 b
    6. dtype: object
  • iloc属性:表示取值和切片都是Python形式的隐式索引;
     
    1. In: data.iloc[1]
    2. Out: "b"
    3. In: data.iloc[1:3]
    4. Out: 3 b
    5. 5 c
    6. dtype: object

Python代码的设计原则之一式“显示优于隐式”。使用lociloc 可以让代码更容易维护,可读性更高。特别是在处理整数索引的对象时,我强烈推荐使用这两种索引器。它们既可以让代码阅读和理解起来更容易,也能避免因误用索引或者切片而产生的小bug

编程要求

本关的编程任务是补全右侧上部代码编辑区内的相应代码,要求实现如下功能:

  • 添加一行数据,时间戳2019-01-29值为320
  • 获取2019-01-04号之后的数据(包含该日期);
  • 最后筛选值大于100的数据,得到以下目标Series对象;
     
    1. 2019-01-06 981
    2. 2019-01-11 647
    3. 2019-01-17 198
    4. 2019-01-20 1698
    5. 2019-01-21 7496
    6. 2019-01-24 8201
    7. 2019-01-29 320
    8. dtype: int64
  • 具体要求请参见后续测试样例。

提示:使用to_datetime()函数可以将字符串转换成时间戳。

pd.to_datetime('2019-01-01')

请先仔细阅读右侧上部代码编辑区内给出的代码框架,再开始你的编程工作!

测试说明

平台会对你编写的代码进行测试,对比你输出的数值与实际正确的数值,只有所有数据全部计算正确才能进入下一关。

测试输入:

 
  1. np.array([4,9,4,3,1,981,13,6,46,1,647,64,31,46,46,13,198,76,13,1698,7496,2,100,8201,30])

预期输出:

 
  1. 2019-01-06 981
  2. 2019-01-11 647
  3. 2019-01-17 198
  4. 2019-01-20 1698
  5. 2019-01-21 7496
  6. 2019-01-24 8201
  7. 2019-01-29 320
  8. dtype: int64


实现代码:

import pandas as pd

import numpy as np

arr = input()

dates = pd.date_range('20190101', periods=25) # 生成时间序列

df = pd.Series(eval(arr),index=dates)

#完成编程要求,并输出结果

#********** Begin **********#

a=pd.to_datetime('2019-01-29')

b=pd.to_datetime('2019-01-04')

df[a]=320

dh=df[b:]

print(dh[dh>100])

#********** End **********

第1关:Series数据选择

任务描述

本关任务:根据相关知识,得到目标Series对象,具体要求请查看编程要求。

相关知识

Series对象与一维Numpy数组和标准Python字典在许多方面都一样。所以,Series对象的数据索引和选择模式与以上两种数据结构类似。

Series数据选择方法

Python中的字典一样,Series对象提供了键值对的映射。

 
  1. In: import pandas as pd
  2. In: data = pd.Series([0.25, 0.5, 0.75, 1.0], index=['a', 'b', 'c', 'd'])
  3. In: data["b"]
  4. Out: 0.5

还可以用Python字典的表达式和方法来检测键/索引和值,也可以像字典一样来修改Series对象的值。

 
  1. In: "a" in data
  2. Out: True
  3. In: data.keys()
  4. Out: Index(['a', 'b', 'c', 'd'], dtype='object')
  5. In: list(data.items())
  6. Out: [('a', 0.25), ('b', 0.5), ('c', 0.75), ('d', 1.0)]
  7. In: data["b"] = 0.05 # 也可以通过此方法来扩展Series
  8. In: data
  9. Out: a 0.25
  10. b 0.05
  11. c 0.75
  12. d 1.00
  13. dtype: float64

Series对象的可变性是一个非常方便的特性:Pandas在底层已经为可能发生的内存布局和数据复制自动决策,用户不需要担心这些问题。

将Series看作一堆数组

Series对象还具备和Numpy数组一样的数组数据选择功能,包括索引、掩码、花哨索引等操作,具体示例如下所示:

  • 将显示索引作为切片:

    注意:显示索引切片结果包含最后一个索引,也就是能取到“c”的值。

     
    1. In: data['a':'c']
    2. Out: a 0.25
    3. b 0.50
    4. c 0.75
    5. dtype: float64
  • 将隐式整数索引作为切片:

    注意:隐式索引切片结果不包含最后一个索引。

     
    1. In: data[0:2]
    2. Out: a 0.25
    3. b 0.50
    4. dtype: float64
  • 掩码:

     
    1. In: data[(data > 0.3) & (data < 0.8)]
    2. Out: b 0.50
    3. c 0.75
    4. dtype: float64
  • 花哨的索引:

     
    1. In: data[["a","e"]]
    2. Out: a 0.25
    3. e 1.25
    4. dtype: float64

索引器:loc和iloc

这些切片和取值操作非常混乱,假如Series对象索引序列为整数时,data[2]不会取第三行,而是取索引序列为2的那一行,也就是说会优先使用显示索引,而data[1:3]这样的切片操作会优先使用隐式索引。

 
  1. In: data = pd.Series(['a', 'b', 'c'], index=[1, 3, 5])
  2. In: data[1]
  3. Out: "a"
  4. In: data[0:2]
  5. Out: 1 a
  6. 3 b
  7. dtype: object

正是应为这些整数索引很容易造成混淆,所以Pandas提供了一些 索引器(indexer) 属性来作为取值的方法。它们不是Series对象的函数方法,而是暴露切片接口的属性。

  • loc属性:表示取值和切片都是显式的;
     
    1. In: data.loc[1]
    2. Out: "a"
    3. In: data.loc[1:3]
    4. Out: 1 a
    5. 3 b
    6. dtype: object
  • iloc属性:表示取值和切片都是Python形式的隐式索引;
     
    1. In: data.iloc[1]
    2. Out: "b"
    3. In: data.iloc[1:3]
    4. Out: 3 b
    5. 5 c
    6. dtype: object

Python代码的设计原则之一式“显示优于隐式”。使用lociloc 可以让代码更容易维护,可读性更高。特别是在处理整数索引的对象时,我强烈推荐使用这两种索引器。它们既可以让代码阅读和理解起来更容易,也能避免因误用索引或者切片而产生的小bug

编程要求

本关的编程任务是补全右侧上部代码编辑区内的相应代码,要求实现如下功能:

  • 添加一行数据,时间戳2019-01-29值为320
  • 获取2019-01-04号之后的数据(包含该日期);
  • 最后筛选值大于100的数据,得到以下目标Series对象;
     
    1. 2019-01-06 981
    2. 2019-01-11 647
    3. 2019-01-17 198
    4. 2019-01-20 1698
    5. 2019-01-21 7496
    6. 2019-01-24 8201
    7. 2019-01-29 320
    8. dtype: int64
  • 具体要求请参见后续测试样例。

提示:使用to_datetime()函数可以将字符串转换成时间戳。

pd.to_datetime('2019-01-01')

请先仔细阅读右侧上部代码编辑区内给出的代码框架,再开始你的编程工作!

测试说明

平台会对你编写的代码进行测试,对比你输出的数值与实际正确的数值,只有所有数据全部计算正确才能进入下一关。

测试输入:

 
  1. np.array([4,9,4,3,1,981,13,6,46,1,647,64,31,46,46,13,198,76,13,1698,7496,2,100,8201,30])

预期输出:

 
  1. 2019-01-06 981
  2. 2019-01-11 647
  3. 2019-01-17 198
  4. 2019-01-20 1698
  5. 2019-01-21 7496
  6. 2019-01-24 8201
  7. 2019-01-29 320
  8. dtype: int64


实现代码:

import pandas as pd

import numpy as np

arr = input()

dates = pd.date_range('20190101', periods=25) # 生成时间序列

df = pd.Series(eval(arr),index=dates)

#完成编程要求,并输出结果

#********** Begin **********#

a=pd.to_datetime('2019-01-29')

b=pd.to_datetime('2019-01-04')

df[a]=320

dh=df[b:]

print(dh[dh>100])

#********** End **********

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论