文章目录
- 1. 创建DataFrame
- 2. 创建数据表
- 3. 创建可变Series表
- 4. 读取csv 数据集
- 5. 保存csv 文件
1. 创建DataFrame
在下面的单元格中,创建一个 DataFrame fruits ,如下所示:
import pandas as pd
# Your code goes here. Create a dataframe matching the above diagram and assign it to the variable fruits.
fruits = pd.DataFrame({'Apples': [30], 'Bananas': [21]})
2. 创建数据表
创建一个与下图匹配的数据框fruit_sales :
# Your code goes here. Create a dataframe matching the above diagram and assign it to the variable fruit_sales.
fruit_sales = pd.DataFrame({'Apples': [35, 41], 'Bananas': [21,34]},index = ['2017 Sales', '2018 Sales'])
fruit_sales
官方代码:
fruit_sales = pd.DataFrame([[35, 41],[21,34]], columns = ['Apples', 'Bananas'],index = ['2017 Sales', '2018 Sales'])
fruit_sales
3. 创建可变Series表
使用如下所示的系列创建可变ingredients :
Flour 4 cups
Milk 1 cup
Eggs 2 large
Spam 1 can
Name: Dinner, dtype: object
ingredients = pd.Series(['4 cups', '1 cup', '2 large', '1 can'], index = ['Flour', 'Milk', 'Eggs', 'Spam'], name = 'Dinner')ingredients
官方代码:
quantities = ['4 cups', '1 cup', '2 large', '1 can']
items = ['Flour', 'Milk', 'Eggs', 'Spam']
recipe = pd.Series(quantities, index=items, name='Dinner')
4. 读取csv 数据集
将以下葡萄酒评论的 csv 数据集读取到名为reviews的 DataFrame 中:
csv 文件的文件路径是 …/input/wine-reviews/winemag-data_first150k.csv。前几行看起来像:
,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
# 让 pandas 使用第一列列作为索引(而不是从头开始创建新列),我们可以指定一个index_col
reviews = pd.read_csv('../input/wine-reviews/winemag-data_first150k.csv', index_col = 0)# Check your answer
q4.check()
reviews
5. 保存csv 文件
运行下面的单元格来创建并显示一个名为animals DataFrame:
animals = pd.DataFrame({'Cows': [12, 20], 'Goats': [22, 19]}, index=['Year 1', 'Year 2'])
animals
在下面的单元格中,编写代码以将此 DataFrame 作为 csv 文件保存到磁盘,名称为cows_and_goats.csv 。
# Your code goes here
animals.to_csv('cows_and_goats.csv')