首页 > 其他分享 >13.DataFrame结构和构造函数

13.DataFrame结构和构造函数

时间:2022-11-15 22:14:46浏览次数:37  
标签:13 randint random DataFrame np 100 构造函数 size

1) DataFrame的创建

最常用的方法是传递一个字典来创建。DataFrame以字典的键作为每一【列】的名称,以字典的值(一个数组)作为每一列。
此外,DataFrame会自动加上每一行的索引(和Series一样)。
同Series一样,若传入的列与字典的键不匹配,则相应的值为NaN。

#数据分析三剑客
import numpy as np 
import pandas as pd 
from pandas import Series, DataFrame 
import matplotlib as mpl
#使用构造函数构造 
DataFrame (data=np. random. randint (0, 100, size=(3, 5)), index=list ("abc"), columns=list ("ABCDE"))

#字典方式构造
dic = {"A": np. random. randint (0, 100, size=3), 
       "B":np. random. randint (0, 100, size=3), 
       "C" :np. random. randint (0, 100, size=3),
       "D": np. random. randint (0, 100, size=3), 
       "E": np. random. randint (0, 100, size=3),}
DataFrame (data=dic, index=list ("abc"))

#使用Series构造DataFram
s = Series (data=np. random. randint (0, 100, size=5), index=list ("abcde"), name="python") 
s
a    18
b     2
c    26
d    74
e    36
Name: python, dtype: int32
df=DataFrame(data=s)
df

 

从文件中读取DataFrame对象

pd. read_csv ()

pd. read_table |

pd. read_excel ()

#读入excel数据
#header 设置excel中,那几行作为列标签,默认就是第一行
#index_col设置excel中,那几列作为行标签
pd.read_excel ("data.xls", header=0, index_col=0)

# sheet_name可以指定索引,也可以指定工作表名称
df=pd.read_excel ("data.xls", header=0, index_col=0,sheet_name="Sheet2")
df

 

标签:13,randint,random,DataFrame,np,100,构造函数,size
From: https://www.cnblogs.com/988MQ/p/16894171.html

相关文章

  • 20201306吴龙灿第十四章学习笔记
    知识点归纳第十四章MySQL数据库系统数据库什么是数据库数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进......
  • 13.判断
    和java一摸一样不适合数值型判断,适合字符串,因为1-100不可能分100个case......
  • simpread-(130 条消息) js 中常见的导入导出方式_AntyCoder 的博客 - CSDN 博客_js 导
    常见的导出方式创建文件foo.js//设置常量constname='tom';constage=20;consthello=function(age){console.log('张三今年'+age)}方式一exportc......
  • CSP 201312-2 ISBN号码 C++
     1#include<iostream>2#include<algorithm>4#include<array>56intmain(){7std::array<int,9>ISBN{};8charc{};9intlenth{......
  • 白嫖永久服务器1668482460013
    阿贝云服务器注册免费领取1核1g内存5m宽带10g内存的云服务器,对于个人来说完全够用了。还有免费备案和虚拟主机,免备案对于搭建个人博客就很方便,部署了小项目上去,运行流畅不......
  • 即时通讯技术文集(第6期):移动端弱网优化文章汇总 [共13篇]
    为了更好地分类阅读52im.net总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第6 期。[- 1 -] 现代移动端网络短连接的优化手段总结:请求速度、弱网适......
  • F2F-L8U3-Describing our job skills 20221113
    Areyoureadyforyourdreamjob?Findthewordsorphraseswiththemeaningsbelow.ffAvailableJOBS......
  • F2F-Dealing with travel delays 20221113
    What'stheworstexperienceyou'vehadwhiletravelling?Describewhathappened.Discusswithapartnerplanetimedelaytraindelay因为疫情的问题我不能回家......
  • 13-1 k8s网络插件CNI
    k8s网络插件CNICNI:ContainerNetworkInterface:最主要功能就是实现Pod资源能够跨宿主机进行通信Flannel:最成熟、最简单的选择Calico:性能好、灵活性最强,目前的企业......
  • 13-3 k8s网络插件-flannel安装
    flannel安装:1、yaml方式安装:在已经安装好k8s集群之上部署flannel:获取flannelyaml文件,应用官方的yaml文件:若以下无法下载:https://kubernetes.io/docs/concepts/cl......