Python2_Pandas库(数据读取)

1.数据读取

food_info.csv数据
在这里插入图片描述

import pandas
food_info=pandas.read_csv("food_info.csv")#read_csv函数读取csv数据文件
print(type(food_info))#DataFrame是pandas的核心
print(food_info.dtypes)#该CSV文件的主要的数据类型
print(help(pandas.read_csv))

<class ‘pandas.core.frame.DataFrame’>

NDB_No int64
Shrt_Desc object
Water_(g) float64
Energ_Kcal int64
Protein_(g) float64
Lipid_Tot_(g) float64
Ash_(g) float64

food_info.head()#默认打印出数据的前五条数据,()内可以指定显示多少行

在这里插入图片描述

food_info.tail(3)#显示后三行

在这里插入图片描述

col=food_info.columns    #把其所有的列名赋值给col
print(col)

在这里插入图片描述

sh=food_info.shape         #当前数据的规模,也就是行列,行代表数据个数,列代表数据的指标
print(sh)

(8618, 36)

2.索引与计算

print(food_info.loc[1])     
#通过loc函数定位数据的索引,获取数据元素的值(一行)

在这里插入图片描述

ndb=food_info["NDB_No"]   #将其整列赋值给ndb
print(ndb)

在这里插入图片描述

col=["NDB_No","Shrt_Desc"]          
 #将数据的两列赋值给col,col作为定位,打印出其两列的值
ndb=food_info[col]
print(ndb)

在这里插入图片描述

col_names=food_info.columns.tolist() #将food_info的列名转为一个list列表
print(col_names)      #打印出列名
gram_columns = []      #定义一个空的列表

for c in col_names:              #循环列表
    if c.endswith("(g)"):        #如果列名以“(g)”结尾
        gram_columns.append(c)   #将此列名追加到gram_columns中
gram_g = food_info[gram_columns]  #找到这些列
print(gram_g.head())

在这里插入图片描述

print(food_info["Iron_(mg)"])
Iron_g = food_info["Iron_(mg)"]*1000       #将此列的值转换为以g为单位了
print(Iron_g)

在这里插入图片描述

print(food_info.shape)
Iron_g = food_info["Iron_(mg)"]*1000
food_info["Iron_(g)"] = Iron_g             #添加一列
print(food_info.shape)

在这里插入图片描述

max_Energ_Kcal = food_info["Energ_Kcal"].max()     #找出此列的最大值
print(max_Energ_Kcal)

在这里插入图片描述

3.常用的预处理方法

food_info.sort_values("Energ_Kcal",inplace=True)       
 #调用sort_values函数,默认是升序排列
print(food_info["Energ_Kcal"])
food_info.sort_values("Energ_Kcal",inplace=True,ascending=False)
#ascending=Flase  升序改为降序排列
print(food_info["Energ_Kcal"])

在这里插入图片描述
在这里插入图片描述
titanic_train.csv的数据值

import numpy as np
import pandas as pd
titanic= pd.read_csv("titanic_train.csv")
titanic.head()

在这里插入图片描述

age=titanic["Age"]
print(age.loc[0:10])

在这里插入图片描述

age_null=pd.isnull(age)        #isnull()判断是否是空值
print(age_null)

在这里插入图片描述

age_null_true = age[age_null]         #找出是空值的项
print(age_null_true)

在这里插入图片描述

age_null_len=len(age_null_true)#数据为空值的个数
 print(age_null_len)      

177

mean_age=age.mean()  #平均值
print(mean_age)

29.69911764705882

passager_fare = titanic.pivot_table(index="Pclass",values="Fare",aggfunc=np.mean)
print(passager_fare)
#pivot_table计算两个相关参数间的关系,这里表示的是Pclass船票的等级和其Fare船票价格的关系
#的反应,以Pclass为索引,求取各类等级船票价格的平均值

在这里插入图片描述

pclass_fare_survived = titanic.pivot_table(index="Pclass",values=["Fare","Survived"],aggfunc=np.sum)
print(pclass_fare_survived)
#pclass船票等级与fare船票总价格以及获救总人数之间的关系

在这里插入图片描述

print(titanic.shape)
new_titanic=titanic.dropna(axis=0,subset=["Age","Sex"])
print(new_titanic.shape)
#dropna为去除函数 去除Age和Sex值为空的数据,

在这里插入图片描述

age83=titanic.loc[83,"Age"]       #定位查找第83个数据的Age年龄值
print(age83)

28.0

4.pandas的自定义函数

new_titanic = titanic.sort_values("Age",ascending=False)
new_titanic.loc[0:10]

在这里插入图片描述

new_titanic_reindex = new_titanic.reset_index(drop=True)
new_titanic_reindex.loc[0:10]       #对其进行排序的数据的索引编号从新进行排序

在这里插入图片描述

def hundred_row(column):           #def定义一个函数找出第100行的数据
    hun_item=column.loc[99]
    return hun_item
h_row=titanic.apply(hundred_row)   #apply调用函数
print(h_row)

在这里插入图片描述

5.Series结构

Series一行或者一列,就是向量,称之为Series
在这里插入图片描述

import pandas as pd
fandango_score_comparison = pd.read_csv("fandango_score_comparison.csv")
series_film=fandango_score_comparison['FILM']
print(type(series_film))         #列类型是Series

在这里插入图片描述

from pandas  import Series
series_film_value=series_film.values
print(type(series_film_value))     #Series内的值的类型是ndarray

class 'numpy.ndarray'>
由此可见pandas是在numpy的基础上进行封装的库

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值