주피터 노트북을 사용해서 filtering 과 ordering 사용
1. import pandas
import
import pandas as pd
2. csv 파일 불러오기
In [2]:3.
#폴더에서 데이터 불러오기
df = pd.read_csv(f'/Users/grace/Desktop/Alex/world_population.csv')
df
df[df['Rank']<=10]
Out[6]:
specific_countries = ['Bangladesh','Brazil']
df[df['Country'].isin(specific_countries)]
#United를 포함하는 열 뽑기
df[df['Country'].str.contains('United')]
#country를 인덱스로 하는 열 뽑기
df2 = df.set_index('Country')
df2
- filter 사용하기( axis 의 경우, column = 1, row =0 => 컬럼을 찾고 싶으면 axis 1 추가 필요)
#'Continent','CCA3'만 filter하는 테이블 만들기(item, axis 사용)
df2.filter(items = ['Continent','CCA3'], axis = 1)
Out[26]:
- axis = 0 (가로 행에서의 정보를 찾을 때)
#'Zimbabwe'만 filter하는 테이블 만들기(item, axis 사용)
df2.filter(items=['Zimbabwe'], axis =0)
Out[30]:
- like 사용하기
df2.filter(like = 'United', axis = 0)
Out[32]:
#United States'정보 가져오기 using loc
df2.loc['United States']
#3번째 행 정보 가져오기 using iloc
df2.iloc[3]
#10 내의 rank를 Continent','Country'로 sorting 한 후, 마지막에 ascending = [false, true] 포함
df[df['Rank']<=10].sort_values(by = ['Continent','Country'],ascending=[False, True])
출처: Alex the analyst
'Programming > python' 카테고리의 다른 글
[python] pandas group by and agg. function (2) | 2023.06.01 |
---|---|
[python] pandas indexes (0) | 2023.06.01 |
[파이썬] 자동 파일 분류 프로그램 만들기 (0) | 2023.05.31 |
[알고리즘] 파이썬 문법 (0) | 2023.04.10 |
[웹 스크래핑] (0) | 2023.04.01 |