# numerical calculation & data framesimport numpy as npimport pandas as pd# visualizationimport matplotlib.pyplot as pltimport seaborn as snsimport seaborn.objects as so# statisticsimport statsmodels.api as sm# pandas optionspd.set_option('mode.copy_on_write', True) # pandas 2.0pd.options.display.float_format ='{:.2f}'.format# pd.reset_option('display.float_format')pd.options.display.max_rows =7# max number of rows to display# NumPy optionsnp.set_printoptions(precision =2, suppress=True) # suppress scientific notation# For high resolution displayimport matplotlib_inlinematplotlib_inline.backend_inline.set_matplotlib_formats("retina")
값을 대체하는 방식
flights = pd.read_csv('data/flights.csv')
# np.whereflights["season"] = np.where(flights["month"].isin([6, 7]), "summer", "other month")# np.where in assign()flights.assign( season =lambda x: np.where(x.month.isin([6, 7]), "summer", "other month"))# apply wifh if-else or functionflights["month"].apply(lambda x: "summer"if x in [6, 7] else"other month")# map with dictionaryflights["month"].map({6: "summer", 7: "summer"}).fillna("other month")# pd.eval: query expression을 활용flights.assign( season =lambda x: np.where(pd.eval('x.month in [6, 7]'), "summer", "other month"))# appply with matchdef get_season(mth):match mth:case6|7:return"summer"case _:return"other month"flights["month"].apply(get_season)