使用字典创建DataFrame 报错if using all scalar values,you must pass an index

本文介绍了使用pandas从字典创建DataFrame时遇到的'if using all scalar values, you must pass an index'错误,并提供了解决方案。同时,讲解了如何向DataFrame横向插入一行数据,如何利用apply函数传递参数,如何将字符串转换为时间格式以及反之,如何替换数据中的问号为缺失值,以及如何进行数据取样。" 105100122,9252322,AutoPublish:自动化多平台文章发布工具,"['自动化工具', '前端开发', '后端开发', 'Web爬虫', 'Python编程']
摘要由CSDN通过智能技术生成

1.从字典创建DataFrame 报错if using all scalar values,you must pass an index

my_dict ={1:'zhangsan',2:'lisi',3:'wangwu'}

pd.DataFrame.from_dict(my_dict)

应加入

pd.DataFrame.from_dict(my_dict,orient='index')

2.向DataFrame中横向插入一行list

如果直接插入会按列插入,因为

[1,2,3,4,5].shape = (5,)

必须转成1行5列

np.array([1,2,3,4,5]).reshape(1,-1)

[[1,2,3,4,5]].shape = (1,5)

3.对Series使用apply函数实现传参

先看看python的原生函数,如何进行多参数操作

# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
series
London      20
New York    21
Helsinki    12
dtype: int64
def subtract_custom_value(x, custom_value):
    return x-custom_value
series.apply(subtract_custom_value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值