본문 바로가기
Python

[Python] Pandas Course on Kaggle - 1

by llHoYall 2022. 1. 15.

This is the solution of pandas course (Creating, Reading and Writing) on Kaggle site.

Setup

import pandas as pd

1. Create a DataFrame

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

2. Create a DataFrame with Index

fruit_sales = pd.DataFrame(
    {"Apples": [35, 41], "Bananas": [21, 34]}, index=["2017 Sales", "2018 Sales"]
)

3. Create a Series

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

4. Create a DataFrame by Reading CSV File

reviews = pd.read_csv(
    "../input/wine-reviews/winemag-data_first150k.csv", 
    index_col=0
)

5. Save DataFrame to CSV File

animals.to_csv("cows_and_goats.csv")

댓글