헷갈리는 것, 자주 쓰는 것 정리
📌
'a\rAa'.find(r'[a-z]\r[A-Z]')
에서 -1을 return함.
import re
text = 'a\rAa'
pattern = r'[a-z]\r[A-Z]'
match = re.search(pattern, text)
if match:
print(f"Match found at index {match.start()}")
else:
print("No match found.")
dropna()
df.dropna(axis=0, how'any, thresh=None, subset=None, inplace=True)
df['columnName'].dropna()
target_value = 'desired_value'
result = df['column_name'] == target_value
desired_rows = df[result]
import pandas as pd
# Create a sample DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
# Define the new order of columns
new_order = ['B', 'A', 'C']
# Reorder the columns
df = df[new_order]
print(df)
column_A_df = df[['A']]
pd.set_option('max_colwidth', 1000) # 크게
pd.set_option('max_colwidth', 30) # 작게
df[df['B'] == 'x']['A']
my_list = ['apple', 'banana', 'cherry']
my_dict = {key: None for key in my_list}
print(my_dict)
>>>
{'apple': None, 'banana': None, 'cherry': None}
전:
for key, value in codeDict.items():
for codes in codelist3:
for code in codes:
if code == key:
codeDict[key] += 1
print(codeDict)
후:
codeDict = {key: value + sum(code == key for codes in codelist3 for code in codes) for key, value in codeDict.items()}
columns=['col1', 'col2', 'col3', 'col4', 'col5']
df = pd.DataFrame(columns=columns)
# 기존 index 유지
df = pd.concat([df1, df2])
# 기존 index 무시
df = pd.concat([df1, df2], ignore_index=True)