Pandas는 Python에서 CSV를 읽습니다.

CSV 파일은 쉼표로 구분된 파일입니다. CSV 파일의 데이터에 액세스하려면 데이터 프레임 형식으로 데이터를 검색하는 Pandas의 read_csv() 함수가 필요합니다.

read_csv() 구문

여기는 팬더는 CSV를 읽습니다 매개변수가 있는 구문.

구문: pd.read_csv (filepath_or_buffer, sep=' ,' , header='infer', index_col=None, usecols=None, 엔진=None, Skiprows=None, nrows=None)

매개변수:

  • filepath_or_buffer : csv 파일의 위치입니다. 파일의 문자열 경로나 URL을 허용합니다.
  • 9월 : 구분 기호를 의미하며 기본값은 ','입니다.
  • 머리글 : int, int의 목록, 열 이름으로 사용할 행 번호 및 데이터의 시작을 허용합니다. 이름이 전달되지 않으면(예: header=None) 첫 번째 열은 0으로, 두 번째 열은 1로 표시됩니다.
  • 사용 콜 : CSV 파일에서 선택한 열만 검색합니다.
  • 으르렁거리다 : 데이터세트에서 표시할 행 수입니다.
  • index_col : 없음인 경우 레코드와 함께 표시되는 색인 번호가 없습니다.
  • 건너뛰기 : 새 데이터 프레임에서 전달된 행을 건너뜁니다.

Pandas read_csv를 사용하여 CSV 파일 읽기

이 기능을 사용하기 전에 팬더 라이브러리에서는 Pandas를 사용하여 CSV 파일을 로드합니다.

파이썬3




# Import pandas> import> pandas as pd> # reading csv file> df> => pd.read_csv(> 'people.csv'> )> print> (df.head())>

산출:

 First Name Last Name Sex Email Date of birth Job Title  0 Shelby Terrell Male [email protected] 1945-10-26 Games developer  1 Phillip Summers Female [email protected] 1910-03-24 Phytotherapist  2 Kristine Travis Male [email protected] 1992-07-02 Homeopath  3 Yesenia Martinez Male [email protected] 2017-08-03 Market researcher 4 Lori Todd Male [email protected] 1938-12-01 Veterinary surgeon 

사용 9월 read_csv()에서

이 예에서는 CSV 파일에 특수 문자를 추가하여 9월 매개변수가 작동합니다.

파이썬3




# sample = 'totalbill_tip, sex:smoker, day_time, size> # 16.99, 1.01:Female|No, Sun, Dinner, 2> # 10.34, 1.66, Male, No|Sun:Dinner, 3> # 21.01:3.5_Male, No:Sun, Dinner, 3> #23.68, 3.31, Male|No, Sun_Dinner, 2> # 24.59:3.61, Female_No, Sun, Dinner, 4> # 25.29, 4.71|Male, No:Sun, Dinner, 4'> # Importing pandas library> import> pandas as pd> # Load the data of csv> df> => pd.read_csv(> 'sample.csv'> ,> > sep> => '[:, |_]'> ,> > engine> => 'python'> )> # Print the Dataframe> print> (df)>

산출:

 totalbill tip Unnamed: 2 sex smoker Unnamed: 5 day time Unnamed: 8 size  16.99 NaN 1.01 Female No NaN Sun NaN Dinner NaN 2 10.34 NaN 1.66 NaN Male NaN No Sun Dinner NaN 3 21.01 3.50 Male NaN No Sun NaN Dinner NaN 3.0 None 23.68 NaN 3.31 NaN Male No NaN Sun Dinner NaN 2 24.59 3.61 NaN Female No NaN Sun NaN Dinner NaN 2 25.29 NaN 4.71 Male NaN No Sun NaN Dinner NaN 4 

read_csv()에서 usecols 사용하기

여기서는 로드할 열 3개(예: [이름, 성별, 이메일])만 지정하고 헤더 0을 기본 헤더로 사용합니다.

파이썬3




df> => pd.read_csv(> 'people.csv'> ,> > header> => 0> ,> > usecols> => [> 'First Name'> ,> 'Sex'> ,> 'Email'> ])> # printing dataframe> print> (df.head())>

산출:

 First Name Sex Email 0 Shelby Male [email protected] 1 Phillip Female [email protected] 2 Kristine Male [email protected] 3 Yesenia Male [email protected] 4 Lori Male [email protected] 

read_csv()에서 index_col 사용

여기서는 섹스 먼저 색인을 생성한 다음 직위 index를 사용하면 간단히 헤더를 다시 색인화할 수 있습니다. index_col 매개변수.

파이썬3




df> => pd.read_csv(> 'people.csv'> ,> > header> => 0> ,> > index_col> => [> 'Sex'> ,> 'Job Title'> ],> > usecols> => [> 'Sex'> ,> 'Job Title'> ,> 'Email'> ])> print> (df.head())>

산출:

 Email Sex Job Title  Male Games developer [email protected] Female Phytotherapist [email protected] Male Homeopath [email protected]  Market researcher [email protected]  Veterinary surgeon [email protected] 

read_csv()에서 nrows 사용하기

여기서는 다음을 사용하여 5개의 행만 표시합니다. 매개변수 확대 .

파이썬3




df> => pd.read_csv(> 'people.csv'> ,> > header> => 0> ,> > index_col> => [> 'Sex'> ,> 'Job Title'> ],> > usecols> => [> 'Sex'> ,> 'Job Title'> ,> 'Email'> ],> > nrows> => 3> )> print> (df)>

산출:

 Email Sex Job Title  Male Games developer [email protected] Female Phytotherapist [email protected] Male Homeopath [email protected] 

read_csv()에서 건너뛰기 사용

그만큼 건너뛰기 CSV에서 일부 행을 건너뛰는 데 도움이 됩니다. 즉, 여기에서는 건너뛰기 행에 언급된 행이 원래 데이터세트에서 건너뛴 것을 확인할 수 있습니다.

파이썬3




df> => pd.read_csv(> 'people.csv'> )> print> (> 'Previous Dataset: '> )> print> (df)> # using skiprows> df> => pd.read_csv(> 'people.csv'> , skiprows> => [> 1> ,> 5> ])> print> (> 'Dataset After skipping rows: '> )> print> (df)>

산출:

Previous Dataset:  First Name Last Name Sex Email Date of birth Job Title  0 Shelby Terrell Male [email protected] 1945-10-26 Games developer 1 Phillip Summers Female [email protected] 1910-03-24 Phytotherapist  2 Kristine Travis Male [email protected] 1992-07-02 Homeopath  3 Yesenia Martinez Male [email protected] 2017-08-03 Market researcher 4 Lori Todd Male [email protected] 1938-12-01 Veterinary surgeon  5 Erin Day Male [email protected] 2015-10-28 Management officer  6 Katherine Buck Female [email protected] 1989-01-22 Analyst 7 Ricardo Hinton Male [email protected] 1924-03-26 Hydrogeologist  Dataset After skipping rows:   First Name Last Name Sex Email Date of birth Job Title  0 Shelby Terrell Male [email protected] 1945-10-26 Games developer 1 Kristine Travis Male [email protected] 1992-07-02 Homeopath  2 Yesenia Martinez Male [email protected] 2017-08-03 Market researcher 3 Lori Todd Male [email protected] 1938-12-01 Veterinary surgeon  4 Katherine Buck Female [email protected] 1989-01-22 Analyst 5 Ricardo Hinton Male [email protected] 1924-03-26 Hydrogeologist