当前位置:   article > 正文

Python2_Pandas库(数据读取)_读取food_coded.csv

读取food_coded.csv

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))
  • 1
  • 2
  • 3
  • 4
  • 5

<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()#默认打印出数据的前五条数据,()内可以指定显示多少行
  • 1

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

(8618, 36)

2.索引与计算

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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"])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

import numpy as np
import pandas as pd
titanic= pd.read_csv("titanic_train.csv")
titanic.head()
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

177

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

29.69911764705882

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

28.0

4.pandas的自定义函数

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/278022
推荐阅读
相关标签
  

闽ICP备14008679号