pandas(day1)

一. 快捷键

python 复制代码
# 蓝色是 非编辑模式
	- 创建单元格
  		- a  向上创建单元格
    	- b  向下创建单元格
      	- dd 删除单元格
		- z 撤回
         - c v x   复制  粘贴  剪切
         - 1~6  标题等级
        - m  切换成 markdown
        - y  切换成 code
        - r  切换成 txt
        - shift+enter : 运行 并向下 选中
        - alt +enter : 运行 并向下创建新的单元格
        - ctrl + enter : 运行当前单元格
# 绿色 是 编辑模式
		- Ctrl + C

二. Series

  • 序列类型,一般代表 表格中的一列(默认)或一行
python 复制代码
Series:序列类型,一般代表 表格中的一列(默认)或一行

pd.Series(date)
data: 必须是一维数据,必须是序列类型(list  tuple str  range)
序列类型: 索引是有顺序的
无序类型: 无索引或索引没有顺序 :dict set

numpy 生成的是 array 数组类型

python 复制代码
# 随机生成一个 1-10 的数字
import random
random.randint(1,10)



np.random.randint(10,100,10)
# array([57, 77, 60, 12, 92, 88, 76, 73, 84, 56])


# 生成 5行5列
nd1 = np.random.randint(10,100,size=[5,5])

array([[28, 71, 82, 47, 62],
       [22, 70, 18, 37, 14],
       [59, 52, 84, 79, 16],
       [89, 39, 45, 32, 70],
       [81, 12, 38, 14, 53]])

三. 写入文件

python 复制代码
# 将内容写入 excel中, 文件打开时是无法写入
s1 = pd.Series(np.random.randint(10,100,10),name="年龄")
s1.to_excel("./s1.xlsx")

四.copy

  • 深度拷贝,在内存重新开辟一块编址,复制原来的基本数据类型再放入新的编址
python 复制代码
df1 = pd.DataFrame(data=ndl,copy=True)

改变ndl的值 ,因为加入了copy的原因, df1的值也不会改变

五. DataFrame

  • pd.DataFrame(data,index,columns)
    • data: 是data = 数据 ,
    • index 是行索引
    • columns 是列索引
    • dataFrame 是列式存储, 列优先 df1"列名"
python 复制代码
df1 = pd.DataFrame(data=ndl,index=list("金木水火土"),columns=list("ABCDE"))

	A	B	C	D	E
金	63	25	96	19	53
木	550	29	70	52	26
水	73	12	52	81	13
火	36	48	27	49	48
土	13	10	74	87	55



df1.to_excel("./df1.xlsx",index=False)
这样会去掉行的标题

	A	B	C	D	E
	63	25	96	19	53
	550	29	70	52	26
	73	12	52	81	13
	36	48	27	49	48
	13	10	74	87	55

dataframe的属性

python 复制代码
df1 是一个5行5列数组

df1.dtypes
检查各个列的数据类型

df1.values
数值的所有值

df1.index
行名

df1.columns
列名

df1.shape
(5,5)


df1.size
25

六. Series

1. 属性: 静态数据(变量)

2. 方法:动态数据(函数)

3. Series的一些属性

python 复制代码
df1["A"].dtype  
数据类型 int32

df1["A"].size 
元素个数


df1["A"].shape (形状) ,如果df.shape (5,5)
这里df1["A"] 是 一列 5个 元素 
(5,) 

df1["A"].ndim  
维度  只有一列 所以是1维

df1["A"].name
列或者行的名称 (series中)

df1["A"].index 
行索引


df1["A"].values
值

七.切片(左闭右开)

  • df dataframe 他是列优先 也可以 df.列.行
  • df\["列","列","列","列"] 获取多列
  • df.loc"行" df.loc\["行","行","列","列"] 他是行优先, 切片是 全闭合
  • df.iloc\[0,3,0,2] (行,列) 切片 :左闭右开
python 复制代码
df=pd.DataFrame(
    np.random.randint(10,100,[5,3]),
    columns=["外功","内功","真气量"],
    index=["金蛇郎君","欧阳锋","丘处机","黄老邪","调兄"]
    
)
df


	       外功	 内功	真气量
金蛇郎君 	52	  10	  64
欧阳锋	    87	  45	  94
丘处机	    68	  45	  82
黄老邪	    99	  53	  90
调兄	    72	  71	  56


df["真气量"]["黄老邪"]
df.真气量.黄老邪



df.loc["黄老邪"]


df.loc[["丘处机","调兄"],["外功","真气量"]]
	   外功	真气量
丘处机	68	82
调兄	72  56

df.iloc[[0,3],[0,2]]
	   外功	真气量
金蛇郎君	52	 64
黄老邪	99	  90

# 反转
df.iloc[::-1,::1]
df.loc[::-1,::1]


切片
df.iloc[0:4]
       外功	 内功	真气量
金蛇郎君 	52	  10	  64
欧阳锋	    87	  45	  94
丘处机	    68	  45	  82
黄老邪	    99	  53	  90



df.loc["金蛇郎君":"调兄"]
       外功	 内功	真气量
金蛇郎君 	52	  10	  64
欧阳锋	    87	  45	  94
丘处机	    68	  45	  82
黄老邪	    99	  53	  90
调兄	    72	  71	  56

八. 数据的读取

python 复制代码
pd.read_csv("./订单数据.csv")

pd.read_csv
filepath_or_buffer : 文件的存储路径
sep : 文件中的分隔符
header : 列名称
None : 没有 列名
[10] : 数据从第10个索引开始
[0,1,2] : 多级索引选中
names : 列名称
index_col : 指定那一列 作为 行索引
None : 自定义行索引
数字 : 指定第几个 列 作为行索引 (一般为数值不重复的列)
[0,1] : 多级行索引
usecols : 挑选使用那些列
engine : C引擎(速度)(默认) 和 Python引擎(精度)
skiprows : 跳过前多少行
nrows : 获取数据的数量
encoding : utf-8

excel读取

python 复制代码
d = pd.read_excel("./电商数据.xlsx",sheet_name=["订单信息","地区信息"])


粘贴板
pd.read_clipboard()

sql读取

python 复制代码
from sqlalchemy import create_engine  #pip install sqlalchemy

#create_engine("mysql+pymysql://root:我的密码@localhost:3306/test1")

coon = create_engine("mysql+pymysql://root:root@localhost/advance_sql?charset=utf8")
pd.read_sql("select * from orderinfo limit 1000", coon)

3.多文件读取

python 复制代码
import glob
filename = glob.glob("./CSV/*.CSV")
[pd.read_csv(x) for x in filename]
csv_data = pd.concat(
        [pd.read_csv(x,encoding="gbk") for x in filename],
        ignore_index = True
    
)


filenames = glob.glob("./混合/*.xlsx")
excel_data = pd.concat(
        [pd.read_excel(x) for x in filenames],
        ignore_index = True
    
)
pd.concat([data,excel_data],ignore_index= True)
相关推荐
MATLAB代码顾问11 小时前
Python Pandas数据分析入门指南
python·数据分析·pandas
themingyi2 天前
Abaqus2024安装python包pandas
开发语言·python·pandas
一晌小贪欢2 天前
第26节:自动化办公——利用 Python 自动生成动态分析报告 (PPT/PDF)
开发语言·python·数据分析·自动化·powerpoint·pandas·数据可视化
留白_3 天前
pandas练习题
python·数据分析·pandas
留白_3 天前
pandas进阶学习
学习·pandas
abcy0712133 天前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
留白_5 天前
pandas文件读取与存储
开发语言·python·pandas
SilentSamsara5 天前
特征工程系统方法论:编码、分箱、交互特征与特征选择
开发语言·人工智能·python·机器学习·青少年编程·信息可视化·pandas
一晌小贪欢5 天前
第22节:相关性分析——协方差、相关系数与热力图解读
开发语言·python·数据分析·pandas·数据可视化
糖果店的幽灵7 天前
Pandas 数据读取与写入(IO 操作)详细总结
pandas