python字典转DataFrame的方法有哪些?
python字典转DataFrame的方法有哪些?
pandas的DataFrame提供了数据分析的功能,可以对数据进行一系列的操作,如果将一个Python中的字典data转化为了Pandas中的DataFrame对象,这样字典就作为了数据源。那你知道如何转换吗?本文介绍python字典转DataFrame的方法:1、将字典转变为Series,再转为DataFrame。2、使用DataFrame.from_dict()函数转换。
方法一:将字典转变为Series,再转为DataFrame。
>>>df=pd.DataFrame(pd.Series(dict),columns=['fruits'])
>>>df=df.reset_index().rename(columns={'index':'id'})
>>>df
idfruits
0aapple
1bbanana
>>>
方法二:使用DataFrame.from_dict()函数转换。
importpandasaspd
row={'name':["index"],"age":[20],"dict":["hunan"]}
data=pd.DataFrame.from_dict(row)
print(data)
输出
nameagedict
0index20hunan
以上就是python字典转DataFrame的两种方法,大家选择其中一种方法使用就可以啦~更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python单例模式的应用场景
python单例模式的应用场景1、应用场景需要频繁创建和销毁的对象;创建花费太多时间或资源,但经常使用的对象;工具类对象;经常访问数据库或文件的...详情>>
2023-11-14 18:58:38
python在协程中增加任务
python在协程中增加任务1、添加一个任务task2=visit_url('http://another.com',3)asynicio.run(task2)2、这2个程序一共消耗5...详情>>
2023-11-14 17:58:42
python如何使用python绘制折线图?
使用python绘制折线图过程1、导入库和设置输入折线图数据importnumpyasnpimportmatplotlib.pyplotasplt#x轴刻度标签x_ticks=['a'...详情>>
2023-11-14 16:51:40
python进程的交流方式
python进程的交流方式1、说明进程之间的第一种交流方式——队列。multiprocessing模块中提供了multiprocessing.Queue,它和Queue.Queue的区别在...详情>>
2023-11-14 14:13:29热门推荐
技术干货






