pandas使用数组和字典创建DataFrame &DataFrame转化为字典

代码示例:

import pandas as pd
from pandas import DataFrame,Series
import numpy as np

#使用数组创建dataframe,默认行索引、默认列索引
df1 = DataFrame(np.random.randint(0,10,(3,3)))
print(df1)
'''
打印:
   0  1  2 
0  8  9  7
1  8  4  8
2  1  5  3
'''
#index执行行索引、columns指定列索引
df2 = DataFrame(np.random.randint(0,10,(3,3)),index=[1,2,3],columns=['q','w','e'])
print(df2)
'''
打印:
   q  w  e
1  1  0  7
2  8  7  5
3  7  9  5
'''

#使用字典创建dataframe(行索引由index决定,列索引由字典的键决定)
city={
    'Province': ['TianJin', 'Beijing', 'ShangHai', 'ChongQing','ShenZhen'],
    'pop': [1.3, 2.5, 1.9, 0.7,2.1],
    'year': [2019, 2019, 2019, 2019, 2019]}
df3 = DataFrame(city)
print(df3)
'''
打印:
    Province  pop  year
0    TianJin  1.3  2019
1    Beijing  2.5  2019
2   ShangHai  1.9  2019
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值