Pandas
-
12/20-Pandas스파르타/TIL(Today I Learned) 2023. 12. 20. 15:32
판다스를 하면서 특정 행만 추출을 하고싶을 때 df [1:3] 을하면 index 1,2가 나와서 Jenny와 Nate가 나오게 된다. 하지만 다시 df를 출력하면 3개의 행이 나오게 된다. 이유는 df를 바꿔주지 않았기 때문 df = df [1:3]를 하면 이제 df를 출력하면 2개의 행만 나온다. ❕ 불연속한 row를 추출하고싶다 슬라이싱은 연속된 데이터를 선택할 때 도움이 되는 기능이고 불 연속한 데이터를 원하면 loc를 사용한다. loc = location이다. 사용법은 df.loc[ [0,2] ] 처럼 쓰면 된다. ❕ DataFrame에 있는 데이터를 조건에 맞춰서 추출하고싶다! 그렇다면 조건을 추가해야한다. df[ df.age >25 ] 처럼 조건을 안에 넣어주면 된다. if 나이가 25 초과이..
-
12/19 - pandas스파르타/TIL(Today I Learned) 2023. 12. 19. 18:03
pandas 공부 중 처음에 파일이 오류가 났다. SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape 여기서 오류는 \UXXXXX 부분에서 \U가 유니코드로 인식이 된다는 말이라고 한다. 해결책은 파일을 업로드하는 파일 명(즉 경로)앞에 r을 붙이고 쓴다. r이 의미하는 것은 Raw문자열이며, 가공되지 않는 문자 그대로를 사용한다~ 라는 뜻이다. ❗ 우리가 만일 csv 파일이 아닌 text파일에 저장된 데이터를 연다고 가정하자 이때 text 파일에 쉼표로 구분 되어있으면 read_csv를 써도 잘 작동된다. 그러나 텍스트 파일에 tab으로 구분된 데이터..