Pandas - 정의, indexing

Gen·2022년 3월 23일
0

python

목록 보기
1/2

I'm going to write about Pandas in English Today.
Because I just want it. :)

what is Pandas?

Pandas is "python data analysis".
Pandas is especially useful to structured data.
Pandas library's unit is basically DataFrame like spreadsheet.

So, Studying Pandas is like Learning dataframe.

First, if you want to use Pandas, you should import as follows:

import numpy as np
import pandas as pd

This is very basic and important thing so you should keep in mind.

DataFrame is 2D table, Seris is a line of rows or columns.
This means that a bunch of Series is DataFrame.

(wow, It's really hard to explain in English as foreigner and python beginner. It takes really long time compared to writing in Korean.
I should stop writing in English... bye😂 )

Essenttial basic functionality

# 통상 dataframe의 의미로 쓰는 df라는 이름으로 Dataframd 함수 생성
 df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['A', 'B', 'C', 'D'])
df

#이거 여기서 값 출력하는 법 있겠지? 흠 🤔

head(), tail()

  • 시리즈 또는 데이터프레임의 일부를 보고 싶을 때 사용
  • 5개를 기본으로 보여줌
  • head()는 맨 위에서부터, tail()은 꼬리에서부터

index, columns, values

df.index
df.columns
df.values

# 나도 모르게 df.index() 이런식으로 괄호를 치려고 하는 경향이 있다. 왜지?
  • columns는 특히 데이터 파악 시 어떤 컬럼이 있는지 한눈에 알고자 할 때 사용하면 좋다.

info()

df.info()
  • dataframe에 대한 전체적인 요약정보를 보여준다.
  • 컬럼 이름, 컬럼에 non-null이 있는지, datatype은 무엇인지 등등

describe()

df.describe()
  • dataframe의 전체적인 통계정보를 보여준다.
  • 수치 데이터 파악할 때 좋은 것 같다.

sort_values()

df.sort_values(ascending=False)
❓여담
오름차순이 자꾸 헷갈린다.
작은 값을 가진 애들이 큰 값을 가진 애들한테 간다고 생각하면 되는데,
순위 관점에서는 1이 100보다 큰 의미니까 이거랑 섞여서 혼자 혼란스러워함.
아직 정리가 안 됐는지 어떨땐 dataframe의 형태인 값이 나오고, 어떨땐 정보 형식(?)으로 나오는지 모르겠음.
문제는 풀리는데 근본적인 게 헷갈리니까 문제를 맞춰서 뿌듯하지 않다.

Indexing

데이터에서 어떤 조건을 만족하는 원소를 찾는 방법

# df에서 column A에 해당하는(조건) 값을 찾아줌
df["A"]

# index의 특정 날짜를 통한 값 찾기 
df.loc['2021-01-03']
#즉, 2021-01-03 index에 해당하는 1개의 row 값(dataframe에서 모든 column을 포함한)을 찾아줌

# 특정 위치를 통한 indexing
df.iloc[2]
#즉, 3번째에 위치한 1개의 row 값을 찾아줌

🎁정리 끝

더 정리하다가는 내일 오전 수업 졸 것 같아서 여기까지.
설명이 구구절절 긴 것 같지만 저렇게 다 적지 않고 간단하게 적으면 나중에 뭔 소린지 모르기 때문에 최대한 이해한만큼 다 적어야 한다.

0개의 댓글