상세 컨텐츠

본문 제목

Pandas: read_csv / DataFrame (with sample code)

Pandas

by Riella 2020. 7. 7. 17:46

본문

728x90
  • filename: name of csv file in string
  • header: header=0 means column names are inferred from the first line of the file
    • header=None column names are passed explicitly
  • index_col: Columns to use as the row labels of the DataFrame
    • (index_col = False <- not to use the first column as the index)
  • usecols: return a subset of the columns.
    • need to correspond to column names provided either by the user in names or inferred from the document header rows.
pip install pandas

This is an example code of using read_csv

## -*- coding: utf-8 -*-
import pandas as pd
import sys

##Copy and paste this code to python version 3 and above
##Save it
##Download School.csv on the same file
##Right Click
##Select Command Promt (명령 프롬프트)
##Type python "[filename].py" "School.csv"
def main(lst):
    column = ["ID", "Name", "School"]
    for i in range(len(lst)):
        csv_file = pd.read_csv(lst[i], header=0, index_col=False, usecols = column)
        print(csv_file)

if __name__ == "__main__":
    #reading filenames from cmd
    if len(sys.argv) > 1:
        lst = []
        for i in range(1, len(sys.argv)):
            lst.append(sys.argv[i])
        main(lst)
    else:
        print("Error: Please Enter Input csv filenames")

If you don't have Pandas, turn on cmd and type below code: (for Windows)

pip install pandas

You may download both csv, above python sample code and run from here

 

DataFrame has data, index, columns, type, copy.

  • data: list-like object (list of dictionary)
  • index: index to use for the resulting frame (default range)
  • columns: column labels for the resulting frame
  • dtype: single data type for the whole data
  • copy: boolean (default=false), and it copy data from inputs

 

댓글 영역