pandas简单使用介绍

1.pandas读取csv文件,并选择数据行,数据列,单个及多个数据

CSV文件

 示例代码:

import numpy as np
import pandas as pd

csv_path = r'C:\Users\10907\Documents\Training\days\csv文件操作\test.CSV'
def pandas_csv(csv_path):
    # df = pd.read_csv(csv_path,sep='',encoding='utf-8',parse_dates=['Purchase Date'],dayfirst=True,index_col='Purchase Date')
    # sep 将列分隔符改成;
    # parse_dates 日期解析
    # dayfirst 将天放在# index_col 将索引设置为日期列
    df = pd.read_csv(csv_path,parse_dates=['Purchase Date'])
    bef_3line = df[:3]  # 读取前3行,切片
    # print(df[2:5]) # 2到5行数据
    # print(bef_3line)
    row = df['Cost']  # 选择一列
    # print(row)
    row_line = df['Cost'][:5]  # 选择一列的前5行
    # print(row_line)
    more_row = df[['Cost','Part Number']]  # 选择多列
    more_row = df[['Cost','Part Number']][:3]  # 选择多列前3行
    # print(more_row)
    row_count = df['Invoice Number'].value_counts() # 将列中的重复值进行分类汇总
    # print(row_count)
    top3 = row_count[:4] # 重复值最多的前3个
    # print(top3)
    namex = df[df['Supplier Name'] == 'Supplier Y']  # 选择Supplier Name列,值等于Supplier X的行
    # print(namex)
    # print(namex[:2]) # 前两行
    name = df['Supplier Name'] == 'Supplier Y' # 符合条件的数据是True,不符合的是False
    pnum = df['Part Number'] == 6650  # 符合条件的数据是True,不符合的是False
    data_and = df[name & pnum]  # Supplier Name列值是Y和Part Number列值为6650的行
    # data_and = df[name & pnum][:2] # 选择结果前2行
    # print(data_and)
    res = df[name & pnum][['Supplier Name','Part Number','Cost','Purchase Date']][:2] # 选择要显示的列
    # print(res)
    # loc的使用,可以同时选择特定的行和列
    # print(df.loc[0]) #第0行数据
    # print(df.loc[0:2]) # 选择1-2行数据
    # print(df.loc[:,['Invoice Number','Part Number']]) # 选择2列数据
    # print(df.loc[0:2,['Invoice Number','Part Number']]) # 选择第1行到第3行,Invoice Number','Part Number两列数据,索引从0开始
    # print(df.loc[2,['Invoice Number','Part Number']]) # 选择第3行,Invoice Number','Part Number两列的数据
    # print(df[df['Purchase Date'] == '2002-03-14']) # 选择日期是2002-03-14的行
    # iloc的使用,iloc用来选择特定的列,可以使用列索引值和列标题来选择列
    # print(df.iloc[2]) # 第2行数据
    # print(df.iloc[3:5,0:2]) # 3到5行,第1,2列数据
    # print(df.iloc[[1,2,4],[3,4]]) # 1,2,4行,3,4列数据
    # print(df.iloc[1:3,:]) # 1,2行,所有列数据
    # print(df.iloc[:,1:3]) #1,3列数据
    # print(df.iloc[1,1]) # 第1行,第1列的第一个数据,不包含索引
    # print(df.iat[3,4]) # 同上
    # 条件过滤
    # print(df[df['Part Number'] > 7000])
    # isin() 过滤某列值所在的行
    # print(df[df['Supplier Name'].isin(['Supplier X','Supplier Z'])])
    # 常用内置函数
    # print(df.head(2))  #查看前2行
    # print(df.tail(3))  # 后3行
    # print(df.index) # 查看索引
    # print(df.columns) # 查看所有列名,列标题
    # print(df.values) # 查看所有值,数据结构为列表嵌套
    # print(df.describe()) # 快速统计汇总
    # print(df.T) # 索引与列标题转换
    # print(df.sort_index(axis=1,ascending=False)) # 按轴进行排序
    # print(df.sort_values(by='Part Number')) #按值进行排序
    # print(df.mean())
    # print(df.mean(1))
    # print(df.apply(np.cumsum))
    # print(df.groupby('Supplier Name').sum()) # 分组并对每个分组执行sum
    # print(df.groupby(['Supplier Name','Invoice Number']).sum()) # 多列分组
    
if __name__ == '__main__':
    pandas_csv(csv_path)

 2. 给CSV文件添加标题

header_list = ['Supplier Name','Invoice Number','Part Number','Cost','Purchase Date']
data_frame = pd.read_csv(csv_path,header=None,names=header_list)  # names给CSV文件添加列标题
print(data_frame)

 

posted @ 2022-03-05 12:13  urls  阅读(35)  评论(0编辑  收藏  举报