1.food.csv

============================================================================================
import pandas
food_info = pandas.read_csv("food_info.csv")#object就是string类型
print(type(food_info))#DataFrame
print(food_info.dtypes)
print(help(pandas.read_csv))
==============
输出:
NDB_No int64
Shrt_Desc object
Water_(g) float64
Energ_Kcal int64
Protein_(g) float64
.........
============================================================================================
food_info.head(3)#默认显示前五行数据,颗根据参数选择
food_info.tail()#末尾几行
print(food_info.columns)#列名
print(food_info.shape)#(8618, 36)
==============
输出:
Index(['NDB_No', 'Shrt_Desc', 'Water_..........]
============================================================================================
#取数据
print(food_info.loc[2])#打印第3行数据
print(food_info.loc[2:3])#打印2到3行
print(food_info.loc[[2,3,5]])#打印第2/3/5行数据
============================================================================================
#按列取数据 如果去两列,就写两个列名,用,隔开
#ndb_col = food_info["NDB_No"]#取列数据
#print(ndb_col)
col_name = "NDB_No"
print(food_info["NDB_No"])#打印出"NDB_No”的列项
print(food_info.shape)
#对所有的列元素操作
print(food_info["NDB_No"]/1000)
#对应元素的运算 water_energy = food_info["Water_(g)"]*food_info["Energ_Kcal"]
iron_gram = food_info["Iron_(mg)"]/1000
food_info["Iron_(temg)"] = iron_gram#增加了一列数据 ,增加前是36列 增加后变成37列
print(food_info.shape)
max_Water = food_info["Iron_(mg)"].max()#取这一列的最大值
print(max_Water)
==============
输出:
0 1001
1 1002
2 1003
3 1004
....................
=========================================================================================
col_names = food_info.columns.tolist()#取所有列名
print(col_names)
gram_columns = [] for c in col_names:
if c.endswith("(g)"):
gram_columns.append(c)#存储以g为结尾的列名
print("====================")
print(gram_columns)
print("====================")
gram_df = food_info[gram_columns]
print(gram_df.head(3))
输出:
['NDB_No', 'Shrt_Desc', 'Water_(g)', 'Energ_Kcal', 'Protein_(g)', 'Lipid_Tot_(g)', 'Ash_(g)', 'Carbohydrt_(g)', 'Fiber_TD_(g)', 'Sugar_Tot_(g)', 'Calcium_(mg)', 'Iron_(mg)', 'Magnesium_(mg)', 'Phosphorus_(mg)', 'Potassium_(mg)', 'Sodium_(mg)', 'Zinc_(mg)', 'Copper_(mg)', 'Manganese_(mg)', 'Selenium_(mcg)', 'Vit_C_(mg)', 'Thiamin_(mg)', 'Riboflavin_(mg)', 'Niacin_(mg)', 'Vit_B6_(mg)', 'Vit_B12_(mcg)', 'Vit_A_IU', 'Vit_A_RAE', 'Vit_E_(mg)', 'Vit_D_mcg', 'Vit_D_IU', 'Vit_K_(mcg)', 'FA_Sat_(g)', 'FA_Mono_(g)', 'FA_Poly_(g)', 'Cholestrl_(mg)', 'Iron_(g)', 'Iron_(temg)']
====================
['Water_(g)', 'Protein_(g)', 'Lipid_Tot_(g)', 'Ash_(g)', 'Carbohydrt_(g)', 'Fiber_TD_(g)', 'Sugar_Tot_(g)', 'FA_Sat_(g)', 'FA_Mono_(g)', 'FA_Poly_(g)', 'Iron_(g)']
====================
Water_(g) Protein_(g) Lipid_Tot_(g) Ash_(g) Carbohydrt_(g) \
0 15.87 0.85 81.11 2.11 0.06
1 15.87 0.85 81.11 2.11 0.06
2 0.24 0.28 99.48 0.00 0.00 Fiber_TD_(g) Sugar_Tot_(g) FA_Sat_(g) FA_Mono_(g) FA_Poly_(g) Iron_(g)
0 0.0 0.06 51.368 21.021 3.043 0.00002
1 0.0 0.06 50.489 23.426 3.012 0.00016
2 0.0 0.00 61.924 28.732 3.694 0.00000
======================================================================================
#排序问题
food_info.sort_values("Water_(g)",inplace = True)#在原位置排序,从小到大排序,升序
print(food_info["Water_(g)"])
food_info.sort_values("Water_(g)",inplace = True,ascending=False)#在原位置排序,降序
print(food_info["Water_(g)"])

2.tatanic.csv

====================================================================================
import pandas as pd
import numpy as np
titanic_survival = pd.read_csv("titanic_train.csv")
titanic_survival.head()#默认打印5行 ================================================================================
age = titanic_survival["Age"]#定位到age
print(age.loc[0:5])#打印0--5的值
age_is_null = pd.isnull(age)
print(age_is_null)
print("===============")
age_null_true = age[age_is_null]
print(age_null_true)
======================
输出:
0 22.0
1 38.0
2 26.0
3 35.0
4 35.0
5 NaN
Name: Age, dtype: float64
0 False
1 False
2 False
3 False
4 False
5 True
6 False
............
=========================
5 NaN
17 NaN
19 NaN
26 NaN
================================================================================
mean_age = sum(titanic_survival["Age"])/len(titanic_survival[["Age"]])
print(mean_age)#当有缺失值的时候,无法进行计算
输出:
nan
================================================================================
good_ages = titanic_survival["Age"][age_is_null == False]#去掉缺失值
print(good_ages)
correct_mean_age = sum(good_ages)/len(good_ages)#求均值
print(correct_mean_age) correct_mean_age = titanic_survival["Age"].mean()#求均值
print(correct_mean_age)
================================================================================
#功能:计算每个等级的船舱的平均价位
passenger_class = [1,2,3]
fares_by_class = {}
for this_class in passenger_class:
plass_rows = titanic_survival[titanic_survival["Pclass"] == this_class]#保存一等船舱的数据
pclass_fares = plass_rows["Fare"]#取出数据中Fare列所有值
fare_for_class = pclass_fares.mean()#对所有数据求均值
fares_by_class[this_class] = fare_for_class#保存每个等级的均值
print(fares_by_class)
输出:
{1: 84.15468749999992, 2: 20.66218315217391, 3: 13.675550101832997}
================================================================================
passenger_survival = titanic_survival.pivot_table(index = "Pclass",values="Survived",aggfunc=np.mean)#index:统计的基准,value:index根什么有关系,
print(passenger_survival)
输出:
Survived
Pclass
1 0.629630
2 0.472826
3 0.242363
================================================================================
passenger_survival = titanic_survival.pivot_table(index = "Pclass",values=["Fare","Survived"],aggfunc=np.mean)
print(passenger_survival) 输出:
Fare Survived
Pclass
1 84.154687 0.629630
2 20.662183 0.472826
3 13.675550 0.242363
================================================================================
#缺失值丢掉
drop_na_columns = titanic_survival.dropna(axis=1)#对纵轴为空的列进行丢弃
print(drop_na_columns)
new_tatanic_survival = titanic_survival.dropna(axis=0,subset=["Age","Sex"])
print(new_tatanic_survival)
new_tanic_survival = titanic_survival.loc[83,"Pclass"]#找出某一个值
print(new_tanic_survival)
================================================================================
new_tatanic_survival = titanic_survival.sort_values("Age",ascending = False)
print(new_tatanic_survival[0:10])
re_tatanic_survival = new_tatanic_survival.reset_index(drop = True)#原来的index索引不要了,重新排
print(re_tatanic_survival)
================================================================================
#定义函数:返回第100行数据
def hundredth_row(column):
hundredth_item = column.loc[99]
# print(hundredth_item)
return hundredth_item
hundredth_row = titanic_survival.apply(hundredth_row)#调用函数 打印第一百行数据
print(hundredth_row)

二、pandas学习的更多相关文章

  1. Pandas学习(二)——双色球开奖数据分析

    学习笔记汇总 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学习(四)–数据的归一化 pandas学习(五)–pa ...

  2. 【转】Pandas学习笔记(二)选择数据

    Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...

  3. Pandas 学习笔记

    Pandas 学习笔记 pandas 由两部份组成,分别是 Series 和 DataFrame. Series 可以理解为"一维数组.列表.字典" DataFrame 可以理解为 ...

  4. Python pandas学习总结

    本来打算学习pandas模块,并写一个博客记录一下自己的学习,但是不知道怎么了,最近好像有点急功近利,就想把别人的东西复制过来,当心沉下来,自己自觉地将原本写满的pandas学习笔记删除了,这次打算写 ...

  5. pandas学习(数据分组与分组运算、离散化处理、数据合并)

    pandas学习(数据分组与分组运算.离散化处理.数据合并) 目录 数据分组与分组运算 离散化处理 数据合并 数据分组与分组运算 GroupBy技术:实现数据的分组,和分组运算,作用类似于数据透视表 ...

  6. pandas学习(创建多层索引、数据重塑与轴向旋转)

    pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...

  7. pandas学习(创建数据,基本操作)

    pandas学习(一) Pandas基本数据结构 Series类型数据 Dataframe类型 基本操作 Pandas基本数据结构 两种常用数据结构: Series 一维数组,与Numpy中的一维ar ...

  8. 18-09-27 pandas 学习02

    如何系统的学习python 中有关数据分析和挖掘相关的库?什么是系统的学习?系统的学习就是一个先搭建只是框架体系,然后不断填充知识看,不断更新迭代的过程. Pandas,numpy,scipy,mat ...

  9. pandas学习(四)--数据的归一化

    欢迎加入python学习交流群 667279387 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学习(四)–数据 ...

  10. Pandas学习(三)——NBA球员薪资分析

    欢迎加入python学习交流群 667279387 学习笔记汇总 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学 ...

随机推荐

  1. Android Studio的第一次经历

    第一个简单APP的制作是从xml开始的,通过在java新建一个empty  activity,并在layout里找到对应的xml文件进行编写.每编写一个xml就要事先新建 一个对应的empty  ac ...

  2. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  3. 知识管理——得到CEO脱不花女士的一次分享

    知识管理--得到CEO脱不花女士的一次分享 近日,公司举办了一场"CKO首席知识官"研讨会,邀请到了得到APP的CEO脱不花女士做了一场精彩的分享,让我深受启发. 分享内容围绕3个 ...

  4. JavaScript ES 模块:现代化前端编程必备技能

    自从 ES 模块被添加到规范中后,JavaScript 中的模块就更加简单了.模块按文件分开,异步加载.导出是用 export 关键字定义的:值可以用 import 关键字导入. 虽然导入和导出单个值 ...

  5. SU模型叠加实景三维模型 用它就可以实现了

    草图大师SketchUp是一套直接面向设计方案创作过程的设计软件,使用SketchUp规划设计师可以从潦草的平面草图开始,创建出想像的任何东西 .虽然市面软件众多,也不能取代SketchUp独有的位置 ...

  6. JAVA中常见的阻塞队列详解

    在之前的线程池的介绍中我们看到了很多阻塞队列,这篇文章我们主要来说说阻塞队列的事. 阻塞队列也就是 BlockingQueue ,这个类是一个接 口,同时继承了 Queue 接口,这两个接口都是在JD ...

  7. 通过lseek产生空洞文件

    //off_t lseek(int fd,off_t offset, int base) 偏移量 搜索的起始位置(文件头(SEEK_SET),当前指针位置(SEEK_CUR),文件尾(SEEK_END ...

  8. 重构rbd镜像的元数据

    这个已经很久之前已经实践成功了,现在正好有时间就来写一写,目前并没有在其他地方有类似的分享,虽然我们自己的业务并没有涉及到云计算的场景,之前还是对rbd镜像这一块做了一些基本的了解,因为一直比较关注故 ...

  9. Apache POI读写Excel

    Apache POI是Apache软件基金会的开放源码函式库,POIAPI给Java程序对Microsoft Office格式档案读和写的功能. 官方文档 [https://poi.apache.or ...

  10. css子选择器 :frist-child :nth-child(n) :nth-of-type(n) ::select选择器

    记录一下前一段时间使用.学习的几种选择器. 1. :frist-child 选择器n 比如<ul><li></li> <li></li> & ...