欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python - pandas读取数据

程序员文章站 2023-04-03 07:54:38
Pandas单纯的研究新库,与时俱进前期准备,命令行执行语句:pip3 install pandas一、读取不传表单名,默认读取第一个表单import pandas as pdfrom tools import project_path# project_path.excel_path是excel地址df=pd.read_excel(project_path.excel_path)# 默认读取前五行的数据data=df.head()print("获取到前五行的值,得...

目录

Pandas

一、读取

1.不传表单名,默认读取第一个表单

 2.传一个表单名,读取数据

3.传多个表单名,读取数据 

4.传单个表单索引,读取数据

5.传多个表单索引,读取数据

6.读取指定的单行

7.根据表头,获取指定的行

8.读取指定的多行,数据会存在嵌套的列表里面

9.读取指定的一个单元格值,传入[行,列]即可

10.读取指定的多个单元格值,传入需要获取的“行”列表,和需要的“列”列表。提取数据逻辑同列表

11.列用列表切片,读取所有行的多个列

12.读取excel的pandas理解的所有行号,列表打印。不是自己写的1234行号

 13.读取excel的每列标题,就是excel第一行

 14.获取表格内随机的“行”数据


Pandas

单纯的研究新库,与时俱进

前期准备,命令行执行语句:pip3 install pandas

import pandas as pd  # 导入模块时对模块进行重命名,也就是给模块起一个别名。

一、读取

1.不传表单名,默认读取第一个表单

import pandas as pd
from tools import project_path
# project_path.excel_path是excel地址
df=pd.read_excel(project_path.excel_path)
# 默认读取前五行的数据
data=df.head()

print("获取到前五行的值,得出二维矩阵:\n{0}".format(data))
print(type(data))
获取到前五行的值,得出二维矩阵:
   CaseId        Title Method  ...                           Expected Error Depend
0       1      查看收藏的素材   post  ...  [1808230976,1811176265,762861009]  pass    NaN
1       2      搜索素材不存在   post  ...                 [200,"success",[]]  pass    NaN
2       3       搜索条件为空   post  ...                 [200,"success",[]]  pass    NaN
3       4       正常搜索素材   post  ...                [1221860542,788631]  pass    NaN
4       5  搜索结果页筛选“商用”   post  ...            [1221860542,1221870437]  pass    NaN

[5 rows x 10 columns]
<class 'pandas.core.frame.DataFrame'>

 2.传一个表单名,读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='material')
# 默认读取前五行的数据
data=df.head()

print("获取到表单页‘material’前五行的值,得出二维矩阵:\n{0}".format(data))
print(type(data))
获取到表单页'material'前五行的值,得出二维矩阵:
   CaseId        Title Method  ...                           Expected Error Depend
0       1      查看收藏的素材   post  ...  [1808230976,1811176265,762861009]  pass    NaN
1       2      搜索素材不存在   post  ...                 [200,"success",[]]  pass    NaN
2       3       搜索条件为空   post  ...                 [200,"success",[]]  pass    NaN
3       4       正常搜索素材   post  ...                [1221860542,788631]  pass    NaN
4       5  搜索结果页筛选“商用”   post  ...            [1221860542,1221870437]  pass    NaN

[5 rows x 10 columns]
<class 'pandas.core.frame.DataFrame'>

 

3.传多个表单名,读取数据 

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name=['material','login'])
# 获取两个表单的所有值
data=df.values

print("获取的值:\n{0}".format(data))
print(type(data))
获取的值:
<built-in method values of dict object at 0x7feb0087d3c0>
<class 'builtin_function_or_method'>

 

4.传单个表单索引,读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name=0)
# 读取该表单所有数据
data=df.values

print("获取的值:\n{0}".format(data))
print(type(data))
获取的值:
[[1 '账号为空' 'post'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json'
  '{"Content-Type":"application/x-www-form-urlencoded"}'
  '{"username":"",\n"password":"111111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}'
  '["code"]' '[-1]' 'pass' nan]
 [2 '密码为空' 'post'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json'
  '{"Content-Type":"application/x-www-form-urlencoded"}'
  '{"username":"33@qq.com",\n"password":"",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}'
  '["code"]' '[-2]' 'pass' nan]
 ]
<class 'numpy.ndarray'>

5.传多个表单索引,读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name=[0,2])
# 读取数据
data=df.values

print("获取的值:\n{0}".format(data))
print(type(data))
获取的值:
<built-in method values of dict object at 0x7f8f394e5500>
<class 'builtin_function_or_method'>

 

6.读取指定的单行

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 读取第一行数据,pandas从表头行下一行开始为0
data=df.loc[0]

# 根据标签取第0行的Title列
# test = df.loc[0,'Title']

print("获取的值:\n{0}".format(data))
print(type(data))
获取的值:
CaseId                                                      1
Title                                                    账号为空
Method                                                   post
Url         https://api.chuangkit.com/login/sSOLogin4Web2....
Headers     {"Content-Type":"application/x-www-form-urlenc...
Data        {"username":"",\n"password":"111111",\n"vcode"...
Keys                                                 ["code"]
Expected                                                 [-1]
Error                                                    pass
Depend                                                    NaN
Name: 0, dtype: object
<class 'pandas.core.series.Series'>
import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 读取第一行数据,pandas从表头行下一行开始为0
data=df.loc[0].values

print("获取的值:\n{0}".format(data))
print(type(data))
获取的值:
[1 '账号为空' 'post'
 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json'
 '{"Content-Type":"application/x-www-form-urlencoded"}'
 '{"username":"",\n"password":"111111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}'
 '["code"]' '[-1]' 'pass' nan]
<class 'numpy.ndarray'>

7.根据表头,获取指定的列

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')

data=df['Title'].values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
['账号为空' '密码为空' '密码错误' '正常登录']
获取的值类型 <class 'numpy.ndarray'>

 

8.读取指定的多行,数据会存在嵌套的列表里面

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 读取指定的多行,要在loc[]里面嵌套列表指定行数
data=df.loc[[0,2]].values

print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
[[1 '账号为空' 'post'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json'
  '{"Content-Type":"application/x-www-form-urlencoded"}'
  '{"username":"",\n"password":"111111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}'
  '["code"]' '[-1]' 'pass' nan]
 [3 '密码错误' 'post'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json'
  '{"Content-Type":"application/x-www-form-urlencoded"}'
  '{"username":"2446@qq.com",\n"password":"1111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}'
  '["code"]' '[-2]' 'pass' nan]]
获取的值类型 <class 'numpy.ndarray'>

9.读取指定的一个单元格值,传入[行,列]即可

Python - pandas读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 读取指定的单元格数据,要在iloc[]里写入单元格的"行列"坐标值
data=df.iloc[0,2]

print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
post
获取的值类型 <class 'str'>

10.读取指定的多个单元格值,传入需要获取的“行”列表,和需要的“列”列表。提取数据逻辑同列表

Python - pandas读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 获取第0行和第2行的第一列数据
data=df.iloc[[0,2],[1]].values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
print('可以再次提取',data[0])
获取的值:
[['账号为空']
 ['密码错误']]
获取的值类型 <class 'numpy.ndarray'>
可以再次提取 ['账号为空']
import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 获取第0行和第2行的第一列,第三列数据
data=df.iloc[[0,2],[1,3]].values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
print('可以再次提取',data[0])
获取的值:
[['账号为空'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']
 ['密码错误'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']]
获取的值类型 <class 'numpy.ndarray'>
可以再次提取 ['账号为空' 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']
 

11.列用列表切片,读取所有行的多个列

Python - pandas读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 获取所有行的第1,3列,利用了列表切片
data=df.iloc[:,[1,3]].values
#data=df.iloc[0:5,[1,3]].values

print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
[['账号为空'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']
 ['密码为空'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']
 ['密码错误'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']
 ['正常登录'
  'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json']]
获取的值类型 <class 'numpy.ndarray'>

12.读取excel的pandas理解的所有行号,列表打印。不是自己写的1234行号

Python - pandas读取数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 获取excel的所有行,列表打印
data=df.index.values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
[0 1 2 3]
获取的值类型 <class 'numpy.ndarray'>

 13.读取excel的每列标题,就是excel第一行

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')

data=df.columns.values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))
获取的值:
['CaseId' 'Title' 'Method' 'Url' 'Headers' 'Data' 'Keys' 'Expected'
 'Error' 'Depend']
获取的值类型 <class 'numpy.ndarray'>

 14.获取表格内随机的“行”数据

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
# 随机获取三行
data=df.sample(3).values
print("获取的值:\n{0}".format(data))
print('获取的值类型',type(data))

14.获取表单所有数据,将数据输出为字典

import pandas as pd
from tools import project_path
df=pd.read_excel(project_path.excel_path,sheet_name='login')
test_data = []

for i in df.index.values:
    data = df.iloc[i,[0,1,2,3,4,5,6,7,8,9]].to_dict()
    test_data.append(data)

print(test_data)
[
{'CaseId': 1, 'Title': '账号为空', 'Method': 'post', 'Url': 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json', 'Headers': '{"Content-Type":"application/x-www-form-urlencoded"}', 'Data': '{"username":"",\n"password":"111111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}', 'Keys': '["code"]', 'Expected': '[-1]', 'Error': 'pass', 'Depend': nan}, 
{'CaseId': 2, 'Title': '密码为空', 'Method': 'post', 'Url': 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json', 'Headers': '{"Content-Type":"application/x-www-form-urlencoded"}', 'Data': '{"username":"246@qq.com",\n"password":"",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}', 'Keys': '["code"]', 'Expected': '[-2]', 'Error': 'pass', 'Depend': nan},
{'CaseId': 3, 'Title': '密码错误', 'Method': 'post', 'Url': 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json', 'Headers': '{"Content-Type":"application/x-www-form-urlencoded"}', 'Data': '{"username":"246@qq.com",\n"password":"1111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}', 'Keys': '["code"]', 'Expected': '[-2]', 'Error': 'pass', 'Depend': nan},
{'CaseId': 4, 'Title': '正常登录', 'Method': 'post', 'Url': 'https://api.chuangkit.com/login/sSOLogin4Web2.do?_dataType=json', 'Headers': '{"Content-Type":"application/x-www-form-urlencoded"}', 'Data': '{"username":"246@qq.com",\n"password":"111111",\n"vcode":"",\n"_dataClientType":"0",\n"client_type":"0"}', 'Keys': '["nickname","vipLevelName","userId"]', 'Expected': '["免费用户",32605017,"你看我好看吗"]', 'Error': 'pass', 'Depend': nan}
]

 

 

 
 

本文地址:https://blog.csdn.net/qq_20427967/article/details/113978161

相关标签: Python自动化