Python|Kaggle机器学习系列之Pandas基础练习题
Posted 海轰Pro
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python|Kaggle机器学习系列之Pandas基础练习题相关的知识,希望对你有一定的参考价值。
前言
Hello!小伙伴!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
初学Python 小白阶段
文章仅作为自己的学习笔记 用于知识体系建立以及复习
题不在多 学一题 懂一题
知其然 知其所以然!
1.
题目
In the cell below, create a DataFrame fruits that looks like this:
解答
题目要求:
创建一个DataFrame结构 组成如图
a = {'Apples':[30],'Bananas':[21]}
fruits=pd.DataFrame(a)
运行结果:
其余解答:
fruits = pd.DataFrame([[30, 21]], columns=['Apples', 'Bananas'])
备注:
pd.DataFrame([[30, 21],[20,21]], columns=['Apples', 'Bananas'])
运行结果为
2.
题目
Create a dataframe fruit_sales that matches the diagram below:
解答
题目要求:
创建一个DataFrame结构 组成如图 赋值给fruit_sales
fruit_sales = pd.DataFrame([[35,21],[41,34]],columns=['Apples','Bananas'],index=['2017 Sales','2018 Sales'])
运行结果:
3.
题目
Create a variable ingredients
with a Series that looks like:
Flour 4 cups
Milk 1 cup
Eggs 2 large
Spam 1 can
Name: Dinner, dtype: object
解答
题目要求:
ingredients为Series结构,具体值见图 , 注意定义name、index
quantities = ['4 cups', '1 cup', '2 large', '1 can']
items = ['Flour', 'Milk', 'Eggs', 'Spam']
ingredients = pd.Series(quantities, index=items, name='Dinner')
运行结果:
4.
题目
Read the following csv dataset of wine reviews into a DataFrame called reviews:
The filepath to the csv file is …/input/wine-reviews/winemag-data_first150k.csv. The first few lines look like:
,country,description,designation,points,price,province,region_1,region_2,variety,winery 0,US,“This tremendous 100% varietal wine[…]”,Martha’s Vineyard,96,235.0,California,Napa Valley,Napa,Cabernet Sauvignon,Heitz 1,Spain,“Ripe aromas of fig, blackberry and[…]”,Carodorum Selección Especial Reserva,96,110.0,Northern Spain,Toro,Tinta de Toro,Bodega Carmen Rodríguez
解答
题目要求:
读取一个csv文件 注意设置index_col=0(第一列为index值)
reviews = pd.read_csv('../input/wine-reviews/winemag-data_first150k.csv', index_col=0)
运行结果:
如果不设置index_col=0
5.
题目
Run the cell below to create and display a DataFrame called animals:
animals = pd.DataFrame({'Cows': [12, 20], 'Goats': [22, 19]}, index=['Year 1', 'Year 2'])
animals
In the cell below, write code to save this DataFrame to disk as a csv file with the name cows_and_goats.csv.
解答
题目要求:
将animals(DataFrame结构)保存为csv文件
animals.to_csv("cows_and_goats.csv")
结语
文章仅作为学习笔记,记录从0到1的一个过程
希望对您有所帮助,如有错误欢迎小伙伴指正~
我是 海轰ଘ(੭ˊᵕˋ)੭
如果您觉得写得可以的话,请点个赞吧
谢谢支持 ❤️
以上是关于Python|Kaggle机器学习系列之Pandas基础练习题的主要内容,如果未能解决你的问题,请参考以下文章
Python|Kaggle机器学习系列之Pandas基础练习题
Python|Kaggle机器学习系列之Pandas基础练习题
Python|Kaggle机器学习系列之Pandas基础练习题
Python|Kaggle机器学习系列之Pandas基础练习题