목록전체 글 (182)
취미가 좋다

딕셔너리로 데이터 프레임 생성하기 각 row는 딕셔너리이고 그 row들을 모아서 리스트를 만든다. 그 리스트로 데이터 프레임을 만들면 아래와 같다. import pandas as pd friend_dict_list = [{'name': 'Jone', 'age': 20, 'job': 'student'}, {'name': 'Jenny', 'age': 30, 'job': 'developer'}, {'name': 'Nate', 'age': 30, 'job': 'teacher'}] df = pd.DataFrame(friend_dict_list) 리스트로 데이터 프레임 생성하기 2차원 리스트를 만들고, 따로 헤드를 만든다. from_records라는 함수로 데이터 프레임을 생성한다. friend_list = [ [..

존재하는 파일로부터 데이터 가져오기 csv 파일로부터 가져올 수 있다. txt 파일로도 가져올 수 있지만, 데이터가 ','로 구분되어 있어야 한다. 데이터가 ',' 로 구분되어있지 않고 다른 구분문자로 되어 있으면, delimiter 인자로 지정해주면 된다. import padas as pd df = pd.read_csv('example.csv') df = pd.read_csv('example.txt', delimiter='\t')# 데이터가 탭으로 구분되어 있을 때 헤드 정보가 없을 때 설정하는 법 파일을 가져올 때 헤드 정보가 없다는 것을 알려주고 따로 넣어준다. df = pd.read_csv('example.csv', header=None) df.columns = ['name', 'age', 'jo..

pandas란 데이터를 수정하고 목적에 맞게 변경하는 python 라이브러리이다. 기본 사용법은 아래와 같다. import pandas as pd# 라이브러리를 가져온다. data_frame = pd.read_csv('example.csv') # csv파일을 가져와서 데이터 프레임을 생성한다. data_frame.head()# 앞 5개의 데이터를 가져온다. data_frame.tail(3)# 뒤에서 3개의 데이터를 가져온다. 데이터 프레임 (Data Frame) 판다스에서 사용하는 2D array로 엑셀과 유사하다. 엑셀로는 프로그램을 만들 수 없고 pandas가 numpy를 사용하여 빠르기 때문에 엑셀로 pandas를 대체할 수 없다. 시리즈 (Series) 데이터 프레임의 각 column을 serie..
https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Kth Smallest Element in a BST - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Solution class Solution: def kthSmallest(self, root, k): def rank(node, ans): if node.left and rank(node.left, ans): return True ans.append(n..
https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Solution from collections import deque class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return root d = deque() d..
https://leetcode.com/problems/maximal-square/ Maximal Square - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Solution class Solution: def maximalSquare(self, matrix): ROW, COL = len(matrix), len(matrix[0]) ans = 0 for i in range(COL): matrix[0][i] = 1 if matrix[0][i]=='1' else 0 ..