1.读取csv文件
1.1读取csv
1.1.1全表读取
df = pd.read_csv('1.csv')
1.1.2根据列的index读取
cols_index= [1] #索引是从0开始,所以这里是第二列的值
df = pd.read_csv('1.csv',usecols=cols_index)
df.head()

1.1.3根据列名读取
cols_name= ['Id','dateTime','name']
df = pd.read_csv('1.csv',usecols=cols_name)
df.head()
1.1.4无表头读取
df = pd.read_csv('1.csv',header = None)

- 这种方式对于没有表头的数据适应
1.1.5有表头读取
- 如果表头有空行可以使用Index来跳过空行
df = pd.read_csv('1.csv',header=1)
df.head()
1.1.6跳行读取
df = pd.read_csv('1.csv',skiprows=1)
df.head()
1.1.7查看列名
df = pd.read_csv('directory.csv')
df.columns

1.2获取表中数据类型
1.2.1 获取所有列的信息
import pandas as pd
df = pd.read_csv('1.csv',header=None)
df.info()

1.2.2获取列数和行数
df = pd.read_csv('directory.csv')
df.shape

1.2.3只查看所有数据类型
df = pd.read_csv('directory.csv')
df.dtypes

1.4根据行数获取数据

-
nrows
选取所需要的行数, -
skiprows
跳过前XX行书的数据, -
header=None
不需要表头
image.png
-names
添加新的表头
1.5获取列的数据类型
print("text_data.dtypes")

- 但是这种方法是Pandas自己推测出来的类型
1.6指定列的类型
data = pd.read_csv("vt_tax_data_2016.tsv",dtype={"zipcode":str})
-
这样就把zipcode的类型指定成为了字符串,在pandas中字符串试试object
image.png
1.7指定缺失数据

- 指定
na_values={"zipcode":0}
的是缺失数据NaN -
isna()
获取确实数据的行数,这里有306行的zipcode都是0
1.8处理损坏的数据

-
error_bad_lines= False
表示跳过出错的行
2.读取xlsx文件
excle_demo = pd.read_excel("fcc_survey.xlsx")
2.1处理特殊格式的excel


- 表格中上面空了2行所以
skiprows =2
就跳过前2行 - 列中又有一列是空着的所以可以指定类列数
usecols = "W:AB,AR"
这个表示从W列到AB,然后在加上AR列
2.2获取excel的不同sheet
- 获取sheet 0,下标是从0开始的
excle_demo = pd.read_excel("fcc_survey.xlsx",sheet_name=0)
- 获取sheet 名为2007
excle_demo = pd.read_excel("fcc_survey.xlsx",sheet_name= '2007')
- 获取全部的sheet,返回一个有序字典key为sheetnam;value为DataFrame
excle_demo = pd.read_excel("fcc_survey.xlsx",sheet_name= None)
2.3 合并结构相同但是名字不同的sheet

excle_demo = pd.read_excel("fcc_survey.xlsx",sheet_name= None)
all_responses = pd.DataFrame()
for sheet_name,frame in excel_demo.items():
frame['Year'] = sheet_name #添加一列名为Year,将sheet_name变为列的值
all_responses = all_responses.append(frame)
2.4处理布尔值(yes,no,false,true,Na)

- 先将所有的Yes/No, 0/1,True/False所在的列的类型全部转换为bool,然后将Yes转为True,由于pandas会自动识别0/1所以不需要设置,但是Na还是会被编码为True
2.5 获取表格所有含有Na列的总数
# Load the data
survey_data = pd.read_excel("fcc_survey_subset.xlsx")
# Count NA values in each column
print(survey_data.isna().sum())
>>>
ID.x 0
HasDebt 0
HasFinancialDependents 7
HasHomeMortgage 499
HasStudentDebt 502
dtype: int64
2.6 处理时间和日期

-
处理标准格式的时间Part1StartTime和Part1EndTime
image.png
-
将 Part2StartDate和Part2StartTime合并成一个有效的时间并且改名
image.png

-
格式化Part2EndTime
image.png
image.png
3.读取数据库
3.1创建数据库链接
# Import sqlalchemy's create_engine() function
from sqlalchemy import create_engine
# Create the database engine
engine = create_engine("sqlite:///data.db")
# View the tables in the database
print(engine.table_names())
3.2 执行sql查询
# Create database engine for data.db
engine = create_engine("sqlite:///data.db")
# Write query to get date, tmax, and tmin from weather
query = """
SELECT date,
tmax,
tmin
FROM weather;
"""
# Make a dataframe by passing query and engine to read_sql()
temperatures = pd.read_sql(query,engine)
# View the resulting dataframe
print(temperatures)
4. 读取json
4.1获取API中的json数据
## Get data from an API
api_url = "https://api.yelp.com/v3/businesses/search"
# Get data about NYC cafes from the Yelp API
response = requests.get(api_url,
headers=headers,
params=params)
# Extract JSON data from the response
data = response.json()
# Load data to a data frame
cafes = pd.DataFrame(data['businesses'])
# View the data's dtypes
print(cafes.dtypes)
4.2 爬取的json保存在本地
response = requests.get(
'https://www.ixigua.com/api/searchv2/complex/json%20pandas/30',
params=params,
cookies=cookies,
headers=headers,
)
rjson = response.json()
##dict-json使用dumps
json_data = json.dumps(rjson)
with open("data.json", "w") as file:
file.write(json_data)
5.pandas提高效率
5.1 提高循环算数的效率iterrows()

-
未使用前,算win percentage
image.png
-
使用iterrows()
image.png
5.2 提高循环算数的效率itertuples()

5.3循环的替换方法.apply()

-
未使用前
image.png
-
使用.apply()
image.png
网友评论