Pandas는 데이터 분석, 조작 및 시각화를 위한 인기 있는 Python 라이브러리입니다. 구조화된 데이터와 구조화되지 않은 데이터를 포함하여 다양한 형식의 데이터를 쉽고 효과적으로 작업할 수 있는 풍부한 도구와 기능을 제공합니다. 이 문서에서는 Python에서 빠르고 효율적으로 데이터 분석을 수행하는 데 사용할 수 있는 일반적인 pandas 작업과 함수에 대한 치트시트를 제공합니다.
import pandas as pd
Pandas 라이브러리를 가져온 후에는 다음 작업과 함수를 사용하여 일반적인 데이터 분석 작업을 수행할 수 있습니다.
pd.read_csv(filename): CSV 파일에서 데이터를 로드합니다.
data.head(): 데이터 프레임의 처음 몇 행을 봅니다.
data.tail(): 데이터 프레임의 마지막 몇 행을 봅니다.
data.describe(): 숫자 열에 대한 요약 통계를 계산합니다.
data.info(): 데이터 프레임의 데이터 유형과 메모리 사용량을 확인합니다.
data.columns: 데이터 프레임의 열을 봅니다.
data['column']: 데이터 프레임의 열을 선택합니다.
data.loc[row_index]: 인덱스를 기준으로 데이터 프레임의 행을 선택합니다.
data.iloc[row_index]: 위치를 기준으로 데이터 프레임의 행을 선택합니다.
data.dropna(): 값이 누락된 행을 삭제합니다.
data.fillna(value): 누락된 값을 주어진 값으로 채웁니다.
data.rename(columns={'old': 'new'}): 데이터 프레임의 열 이름을 바꿉니다.
data.sort_values(by='column'): 열의 값을 기준으로 데이터 프레임을 정렬합니다.
data.groupby('column')['column'].mean(): 열의 값으로 데이터 프레임을 그룹화하고 다른 열의 평균을 계산합니다.
"""_summary_
얕은 복사(shallow copy)
list의 슬라이싱을 통한 새로운 값을 할당해봅니다.
아래의 결과와 같이 슬라이싱을 통해서 값을 할당하면 새로운 id가 부여되며, 서로 영향을 받지 않습니다
"""
print("\n","*" * 30, "\n 얕은 복사(shallow copy) \n","*" * 30)
a = [1, 2, 3]
b = a[:]
print(" a = ", a)
print(" b = ", b)
print(" id(a) : ", id(a)) #다른 주소
print(" id(b) : ", id(b))
print(" a == b : ", a == b)
print(" a is b : ", a is b)
b[0] = 5
print(a)
print(b)
"""
하지만, 이러한 슬라이싱 또한 얕은 복사에 해당합니다.
리스트안에 리스트 mutable객체 안에 mutable객체인 경우 문제가 됩니다.
id(a) 값과 id(b) 값은 다르게 되었지만, 그 내부의 객체 id(a[0])과 id(b[0])은 같은 주소를 바라보고 있습니다
"""
a = [[1,2],[3,4]]
b = a[:]
print(" id(a) : ", id(a)) #다른 주소
print(" id(b) : ", id(b))
print(" id(a[0]) : ",id(a[0])) #같은 주소
print(" id(b[0]) : ",id(b[0]))
"""깊은 복사(deep copy)
깊은 복사는 내부에 객체들까지 모두 새롭게 copy 되는 것입니다.
copy.deepcopy메소드가 해결해줍니다
"""
print("\n","*" * 30, "\n deepcopy \n","*" * 30)
import copy
a = [[1,2],[3,4]]
b = copy.deepcopy(a)
a[1].append(5)
print(" a : ", a)
print(" b : ", b)
print(" id(a) : ", id(a)) #다른 주소
print(" id(b) : ", id(b))
>>> x = {1, 2, 3}
>>> x
{1, 2, 3}
>>> id(x)
4396095304
>>> x|={4,5,6}
>>> x
{1, 2, 3, 4, 5, 6}
>>> id(x)
4396095304
str은 immutable 입니다.
s 변수에 첫번째 글자를 변경 시도하면 에러가 발생합니다.
s에 다른 값을 할당하면, id가 변경됩니다. 재할당은 애초에 변수를 다시할당하는 것이므로 mutable과 immutable과는 다른 문제입니다. list또한 값을 재할당하면 id가 변경됩니다.
>>> s= "abc"
>>> s
'abc'
>>> id(s)
4387454680
>>> s[0]
'a'
>>> s[0] = 's'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = 'def'
>>> s
'def'
>>> id(s)
4388970768
2. 변수 간 대입
2-1 mutable한 객체의 변수 간 대입
list의 얕은 복사를 확인 해봅니다.
b 에 a를 할당하면 값이 할당되는 것이 아니라 같은 메모리 주소를 바라봅니다.
b를 변경하면 같이 a도 바뀝니다.
mutable한 다른 객체 또한 똑같은 현상이 나타납니다.
>>> a = [1, 2, 3]
>>> b = a # shallow copy
>>> b[0]= 5
>>> a
[5, 2, 3]
>>> b
[5, 2, 3]
>>> id(a)
4396179528
>>> id(b)
4396179528
2-2 immutable한 객체의 변수간 대입
str 문자열의 얕은 복사를 확인해봅니다.
list와 똑같이 b를 a에 할당하면 같은 메모리 주소를 바라보게 됩니다.
하지만 b에 다른 값을 할당하면 재할당이 이루어지며 메모리 주소가 변경됩니다.
고로 a와 b는 다른 값을 가집니다.
>>> a = "abc"
>>> b = a
>>> a
'abc'
>>> b
'abc'
>>> id(a)
4387454680
>>> id(b)
4387454680
>>> b = "abcd"
>>> a
'abc'
>>> b
'abcd'
>>> id(a)
4387454680
>>> id(b)
4396456400
3. 얕은 복사(shallow copy)
list의 슬라이싱을 통한 새로운 값을 할당해봅니다.
아래의 결과와 같이 슬라이싱을 통해서 값을 할당하면 새로운 id가 부여되며, 서로 영향을 받지 않습니다.
>>> a = [1,2,3]
>>> b = a[:]
>>> id(a)
4396179528
>>> id(b)
4393788808
>>> a == b
True
>>> a is b
False
>>> b[0] = 5
>>> a
[1, 2, 3]
>>> b
[5, 2, 3]
하지만, 이러한 슬라이싱 또한 얕은 복사에 해당합니다.
리스트안에 리스트 mutable객체 안에 mutable객체인 경우 문제가 됩니다.
id(a)값과id(b)값은 다르게 되었지만, 그 내부의 객체id(a[0])과id(b[0])은 같은 주소를 바라보고 있습니다.
Whether you’re a seasoned developer or a curious beginner just starting, creating outstanding websites needs more than just stunning animations and interesting effects.
It all comes down to a solid basis in important concepts.
Mastering these bad boys will make you a more effective and flexible developer, ready to take on every task.
So grab your favorite coding cup (coffee for all-nighters, anyone?), and let’s get started!
Have aBIG IDEAin mind?Let’s discusswhat we can gain together.
Take these three to be the web’s basic building blocks.
HTML organizes your content, CSS beautifully styles it, and JavaScript adds interaction and personality.
Here is a basic breakdown:
HTML(Hypertext Markup Language) is the basis for your website, specifying elements such as headers, paragraphs, and illustrations.
CSS(Cascading Style Sheets): CSS makes your website visually attractive. CSS pseudo-classes can offer dynamic effects when a button is hovered over or focused, such as changing its color or adding stunning animations.
Actionable Tip: Use the BEM (Block-Element-Modifier) structure to write clean, maintainable CSS.
JavaScript: This is the magic that allows webpages to interact with one another. Learn how to write clean, maintainable JavaScript to avoid code challenges in the future.
02. Responsive Web Design
Imagine how your website looks on a large desktop display but not on a mobile device.
Not cool.
Responsive design guarantees that your website works effortlessly on any device, especially PCs, tablets, and smartphones.
Here is the secret sauce:
Media Queriesare like magic spells that tell your website to customize its layout depending on screen size.
Fluid Grids: Imagine a website layout as a grid. Fluid grids use percentages rather than set pixels, allowing the grid to “flow” and adjust to different displays.
Branches: Assume you want to test out a new feature without impacting the main code. Branches let you work on changes separately before merging them back into the main codebase once you’re satisfied.
The web is all about communication! HTTP (Hypertext Transfer Protocol) is the language that computers use to communicate with one another.
When you visit a website, your browser sends an HTTP request, and the server returns an HTTP response giving the website content.
HTTPS (Hypertext Transfer Protocol Secure)is the secure version of HTTP, which encrypts data transport to safeguard your website and user information. Always use HTTPS to ensure security!
APIs (Application Programming Interfaces)are similar to waiters at a restaurant. They accept your request (such as collecting user data) and deliver the information you want from another system. Understanding APIs is essential for creating interactive web apps.
05. Basic SEO:
Do you want your website to be the first thing visitors see when searching for something? Basic Search Engine Optimization (SEO) can help!
Meta Tags: These are hidden messages for search engines that include information about your website’s content.
Keywords: These are the terms that people are likely to look for. Use relevant keywords strategically throughout your website’s content.
Website Performance Optimization: A slow website is a sad one. Optimize your website’s image size and code structure for faster loading, which benefits both search engines and visitors.
06. Web Accessibility
The web should be accessible to everyone! Web accessibility means that persons with disabilities can get to and use your website.
Semantic HTMLmeans using HTML elements to explain the meaning and purpose of your content, instead of merely displaying it.
Keyboard Navigation: A mouse is not used by everyone. Ensure that your website can be accessed just by keyboard.
07. Performance Optimization
No one loves a slow website! Optimize your website’s speed by reducing HTTP requests (the number of times it asks the server for something), adding caching (storing frequently used items locally), and optimizing images for more quickly loading.
Remember that a quick website means a happy user (and a happy search engine!).
Pro Tips & Best Practices: From a Developer to You
Here are some gold pieces I’ve picked up along my journey.
Always comment on your code! Your future self (or someone else) will thank you.