AITC Wiki

practice PandasCSV solution

Pandas CSV 练习参考答案

practice PandasCSV solution

中文版:Pandas CSV 练习参考答案

Start from reading CSV

  1. What is a .csv file? Try opening nba.csv (Ensure the file is saved in your working directory) using Excel and observe its structure.(no code)

  2. Import the pandas library in Python and use pd as an alias.

# hint: how to import numpy: import numpy as np
 
import pandas as pd
  1. Read the nba.csv file using pandas and store it in a DataFrame named df
# hint: pd.read_csv(file_name_string)
 
df = pd.read_csv('nba.csv')
  1. Print the shape of df (number of rows and columns) and list all column names. Answer the questions: how many persons are listed in the df, and how many types of information for each person in the df, and what are they?
# hint: df.shape df.columns.tolist()
print("sizes of dimensions(rows, cols):", df.shape)
print("types of information:", df.columns.tolist())
  1. Print the data types of each column (e.g., int, float, string), and check the memory usage of df.
# hint: df.dtypes
print("data type for cols:\n", df.dtypes)
print("memory usage:")
df.info(memory_usage="deep")
  1. Print all information of the first person (= the first row).
# hint: df.iloc[index]
print("information of first person:\n", df.iloc[0])
  1. Print the last five rows and compare with the original nba.csv file to verify correct data loading.
# hint: df.iloc[:]
print("the last five rows:\n", df.iloc[-5:])