Pandas 中Series , Dataframe相关操作

1.# 请将"_"处替换成能实现注释中功能的代码。

# 示例:
# #设置列表lst中位置3的值为1
# lst_
# 根据注释中说明可知,此处"_"应替换成[3]=1,即有
# lst[3]=1

请先将下面的代码复制到答题框(注意添加缩进):
# 从pandas库导入Series,DataFrame
from pandas import Series, DataFrame

def fact():

answer = []
# 创建字典data
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

# 创建列表col,index1
col = ['year', 'state', 'pop', 'debt']
index1 = ['one', 'two', 'three', 'four', 'five']

# 利用data创建DataFrame对象frame1,并指定该对象列为col,索引为index1
frame1 = _
answer.append(frame1.copy())

# 排序
# 根据索引,对frame1进行降序排序,并指定轴为1
frame2 = frame1._
answer.append(frame2.copy())

# 根据值,对frame1的year列进行排序(升序)并打印
frame3 = frame1._
answer.append(frame3.copy())

# 处理缺失数据
# 对于frame1,只要有某行有NaN就全部删除
frame4 = frame1._
answer.append(frame4.copy())

# 对于frame1,某行全部为NaN才删除
frame5 = frame1._
answer.append(frame5.copy())

# 填充缺失数据
# 对于frame1,将元素为NaN替换成0
frame6 = frame1._
answer.append(frame6.copy())

return answer

# 从pandas库导入Series,DataFrame
import numpy as np
from pandas import Series, DataFrame

def fact():

    answer = []
# 创建字典data
    data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
    'year': [2000, 2001, 2002, 2001, 2002],
    'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

# 创建列表col,index1
    col = ['year', 'state', 'pop', 'debt']
    index1 = ['one', 'two', 'three', 'four', 'five']

# 利用data创建DataFrame对象frame1,并指定该对象列为col,索引为index1
    frame1 = DataFrame(data,columns=col,index=index1)
    answer.append(frame1.copy())

# 排序
# 根据索引,对frame1进行降序排序,并指定轴为1
    frame2 = frame1.sort_index(ascending=True,axis=1)
    answer.append(frame2.copy())

# 根据值,对frame1的year列进行排序(升序)并打印
    frame3 = frame1.sort_values(by=['year'],ascending=False)
    answer.append(frame3.copy())

# 处理缺失数据
# 对于frame1,只要有某行有NaN就全部删除
    frame4=frame1.dropna()
    answer.append(frame4.copy())

# 对于frame1,某行全部为NaN才删除
    frame5 = frame1.dropna(how = 'all')
    answer.append(frame5.copy())

# 填充缺失数据
# 对于frame1,将元素为NaN替换成0
    frame6 = frame1.replace(np.NaN,0)
    answer.append(frame6.copy())

    return answer

2.# 请将"_"处替换成能实现注释中功能的代码。

# 示例:
# #设置列表lst中位置3的值为

# 从pandas库导入Series,DataFrame
from pandas import Series, DataFrame

def fact():
answer =[]
# 创建列表lst
lst = [4, 7, -5, 3]

# 使用列表list生成Series对象obj
obj =_
answer.append(obj.copy())

# 创建数组index
index1 = ['d', 'b', 'a', 'c']

# 创建数据为lst,索引为index1的Series对象obj2
obj2=_
answer.append(obj2.copy())

# 将obj2中索引值为d对应的值赋值为6
obj2_
answer.append(obj2.copy())

# 将obj2中索引值为d对应的值存储到ans1中
ans1_
answer.append(ans1)

# 从obj2找出大于0的元素并存储到ans2中
ans2_
answer.append(ans2)

# 创建字典sdata
sdata = {'Ohio': 45000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}

# 利用sdata生成Series对象obj3
obj3= _
answer.append(obj3.copy())

# 创建列表states
states = ['California', 'Ohio', 'Oregon', 'Texas']

# 创建数据为sdata,索引为states的Series对象obj4
obj4= _
answer.append(obj4.copy())

# 将obj3和obj4进行相加,相同索引部分相加,存储到obj5
obj5 = _
answer.append(obj5.copy())

# 指定obj4的名字为population
obj4_
answer.append(obj4.copy())

# 指定obj4的索引的名字为state
obj4_
answer.append(obj4.copy())
return answer

# 从pandas库导入Series,DataFrame
from pandas import Series, DataFrame
def fact():
    answer =[]
# 创建列表lst
    lst = [4, 7, -5, 3]

# 使用列表list生成Series对象obj
    obj = Series(lst)
    answer.append(obj.copy())

# 创建数组index
    index1 = ['d', 'b', 'a', 'c']

# 创建数据为lst,索引为index1的Series对象obj2
    obj2= Series(lst,index=index1)
    answer.append(obj2.copy())

# 将obj2中索引值为d对应的值赋值为6
    obj2['d']=6
    answer.append(obj2.copy())
    
# 将obj2中索引值为d对应的值存储到ans1中
    ans1=obj2['d']
    answer.append(ans1)

# 从obj2找出大于0的元素并存储到ans2中
    ans2=[]
    for i in obj2.index:
        if obj2[i]>0:
            ans2.append(obj2[i])
    answer.append(ans2)

# 创建字典sdata
    sdata = {'Ohio': 45000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}

# 利用sdata生成Series对象obj3
    obj3= Series(sdata)
    answer.append(obj3.copy())

# 创建列表states
    states = ['California', 'Ohio', 'Oregon', 'Texas']

# 创建数据为sdata,索引为states的Series对象obj4
    obj4= Series(sdata,index=states)
    answer.append(obj4.copy())

# 将obj3和obj4进行相加,相同索引部分相加,存储到obj5
    obj5 = obj3+obj4
    answer.append(obj5.copy())

# 指定obj4的名字为population
    obj4.name='population'
    answer.append(obj4.copy())

# 指定obj4的索引的名字为state
    obj4.index.name='state'
    answer.append(obj4.copy())
    return answer

()-

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值