Pandas - Creating, Reading and Writing

hyojeong_sss·2021년 3월 28일
0

introduction (Setup)

import pandas as pd
pd.set_option('max_rows', 5)
from learntools.core import binder.bind(globals())
from learntools.pandas.creating_reading_and_writing import *
print("Setup complete")

Exercises

1. In the cell below, createa DataFrame fruits that looks like this:

1번째 방법

fruits = pd.DataFrame([[30, 21]], columns = ['Apples', 'Bananas'])

2번째 방법

fruits = pd.DataFrame({'Apples':[30], 'Bananas':[21]})

2. Create a dataframe fruit_sales that matches the diagram below:

1번째 방법

fruit_sales = pd.DataFrame([[35, 32], [41, 34]], index = ['2017 Sales', '2018 Sales'], columns = ['Apples', 'Bananas'])

2번째 방법

fruit_sales = pd.DataFrame({'Apples': [35, 41], 'Bananas' : [21, 34]}, 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

1번째 방법

ingredients = pd.Series(['4 cups', '1 cup', '2 large', '1 can'], index = ['Flour', 'Milk', 'Eggs', 'Spam'], name = 'Dinner')

2번째 방법

measurement = ['4 cups', '1 cup', '2 large', '1 can']
food = ['Flour', 'Milk', 'Eggs', 'Spam']
ingredients = pd.Series(measurement, index = food, 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
reviews = pd.read_csv('../input/wine-reviews/winemag-data_first150k.csv', 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', 'Year2'])
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.to_csv('cows_and_goats.csv', header=True)
profile
컴공생

0개의 댓글