网站 扩展,设计师网络平台,阿里巴巴做外贸的网站,wordpress网站模版目录
0 总结
0.1pd.Dataframe有一个比较麻烦琐碎的地方#xff0c;就是引号 和括号
0.2 pd.Dataframe关于括号的原则
0.3 分清楚几个数据类型和对应的方法的范围
0.4 几个数据结构的构造关系
list → np.array(list) → pd.Series(np.array)/pd.Dataframe
1 python 里…目录
0 总结
0.1pd.Dataframe有一个比较麻烦琐碎的地方就是引号 和括号
0.2 pd.Dataframe关于括号的原则
0.3 分清楚几个数据类型和对应的方法的范围
0.4 几个数据结构的构造关系
list → np.array(list) → pd.Series(np.array)/pd.Dataframe
1 python 里的 pandas.Dataframe
2 pd.concat() 可以合并 pd.Dataframe
2.1 pd.concat() 合并规则
3 pd.Dataframe.drop() 删除行列的操作
4 pd.Dataframe 列操作
5 pd.Dataframe 行操作
5.1 sample_dataframe2.head(n2) 取前面的n行不能任意
5.2 sample_dataframe2.query(查询条件)取前面的n行不能任意
6 可以用pd.Dataframe().query() 方法 同时进行行和列筛选
7 序列 pandas.Series(
7.1 什么是序列
7.2 将pd.Dataframe取出1列会变成pd.Series
7.3 序列 pd.series 和数组array() 的转化 0 总结
0.1pd.Dataframe有一个比较麻烦琐碎的地方就是引号 和括号
用的比较多记住这个原则 pd.Dataframe所有的方法里基本都是加一个括起来基本就够了很少有多处多重引号的。 0.2 pd.Dataframe关于括号的原则
关于括号记住一个原则1层括号一般表示一维数组比如pd.Dataframe[]取出来的一般都是1列/1行等2层括号一般表示2维数组比如pd.Dataframe[[]] 取出来的一般都是一个子二维表有些地方需要多层的中括号[] 比如 [ [ ] ] 0.3 分清楚几个数据类型和对应的方法的范围
python原生的
原生类型列表listlist[1,2,3], 原生方法 range(1,10,1)
numpy和 pandas都是python的大包numpy里
对应的数据类型数组arrayarr1np.array([1,2,3])专有方法np.arange(1,10,1)np.arange(start1,stop10,step1) 0.4 几个数据结构的构造关系
list → np.array(list) → pd.Series(np.array)/pd.Dataframe
python原生的
列表listlist1[1,2,3],
numpy里
直接用列表生成np的数组array arr1np.array(list1)
pandas里
用 np.array 为内容直接生成pd.Seriespd.Series(np.array())用 np.array 为列生成pd.Dataframe({key1: np.array(),key2: np.array()})取出pd.Dataframe的某列生成pd.Seriespd.Series.values() np.array 1 python 里的 pandas.Dataframe
本质是一个二维表特殊点在于多了一个默认的序号列语法pd.Dataframe({key1:value1,key2:value2}) 2 pd.concat() 可以合并 pd.Dataframe
2.1 pd.concat() 合并规则
pd.concat() 语法pd.concat([pd.Dataframe1,pd.Dataframe1],axis0/1) pd.concat() 可以指定合并的方向默认是axis0也就是按行的方向合并pd.concat() 可以指定合并的方向如果是axis1就是按列的方向进行合并
import numpy as np
import pandas as pd
import scipy as sp# 可以用list 生成np.array()
sample_array1np.array([1,2,3])
sample_array2np.array([10,20,30])
sample_array3np.array([100,200,300])# 进一步可以用np.array()生成pd.Series
# 注意pd.Series 首字母一定大写
sample_series1pd.Series(sample_array1)
print(sample_series1)
print()# 进一步也可以用np.array()生成pd.DataFrame
# 注意pd.DataFrame 首字母一定大写
sample_dataframe1pd.DataFrame({col1:sample_array1,col2:sample_array2,col3:sample_array3,})
print(sample_dataframe1)
print()sample_dataframe2pd.DataFrame({col1:sample_array1,col2:sample_array21,col3:sample_array31,})
print(sample_dataframe2)
print()print(pd.concat([sample_dataframe1,sample_dataframe2])) # pd.concat()默认合并是axis0, 按行合并
print()print(pd.concat([sample_dataframe1,sample_dataframe2],axis1))
print() 3 pd.Dataframe.drop() 删除行列的操作
pd.Dataframe.drop()pd.Dataframe.drop(行名/列名,axis0/1)axis0 是行注意列名一般是字符串如 col1注意行名一般是数字如 1 4 pd.Dataframe 列操作
pd.Dataframe 数据帧操作列的办法有两种
直接引用 pd.Dataframe 对象的属性pd.Dataframe.列名不加字符串引号类切片的列操作方法pd.Dataframe[列名1]pd.Dataframe[[列名1,列名2,列名3]] #注意是双层中括号 5 pd.Dataframe 行操作
行操作有两种方法sample_dataframe2.head() 方法sample_dataframe2.query()方法 5.1 sample_dataframe2.head(n2) 取前面的n行不能任意
n 只能是前面的连续列
print(sample_dataframe2)
print()
print(sample_dataframe2.head(n2))5.2 sample_dataframe2.query(查询条件)取前面的n行不能任意
sample_dataframe2.query(查询条件)sample_dataframe2.query(可以是任意的一个行条件不要求非是index的值)sample_dataframe2.query(条件1 | 条件2) # or 关系sample_dataframe2.query(条件1 条件2) # and关系 6 可以用pd.Dataframe().query() 方法 同时进行行和列筛选 print(sample_dataframe2.query(col3301)[[col2,col3]]) 7 序列 pandas.Series( 7.1 什么是序列 特殊之处默认带一个序号列可以认为是带 序号的 数组/列表pandas.Series( data, index, dtype, copy) data输入的数据可以是列表、常量、ndarray 数组等。 index索引值必须是唯一的与data的长度相同默认为np.arange(n) dtype数据类型 copy是否复制数据默认为false 7.2 将pd.Dataframe取出1列会变成pd.Series
将pd.Dataframe取出1列会变成pd.Series也就是说 pd.Series 是 pd.Dataframe 的其中1列注意方法不同有差别如果是单取出1列生成pd.Series如果是单取出多列生成的只是更小的pd.Dataframe并不是pd.Series很好理解不要搞错。
print(sample_dataframe2)
print()
print(sample_dataframe2.col2)
print()
print(sample_dataframe2[col2])
print()
print(sample_dataframe2[[col2]])
print()print(type(sample_dataframe2))
print()
print(type(sample_dataframe2.col2))
print()
print(type(sample_dataframe2[col2]))
print()
print(type(sample_dataframe2[[col2]]))7.3 序列 pd.series 和数组array() 的转化
pd.series.values 即可以生成对应的 np.array() 数组
print(sample_dataframe2)
print()
print(sample_dataframe2.col2)
print()
print(sample_dataframe2.col2.values)
print()print(type(sample_dataframe2))
print()
print(type(sample_dataframe2.col2))
print()
print(type(sample_dataframe2.col2.values))
print()
文章转载自: http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.kflpf.cn.gov.cn.kflpf.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn