파이썬/파이썬(python) 중급

map, pandas, pyplot, matplotlib, code

Olivia-BlackCherry 2023. 5. 5. 19:30

map(적용할 함수, iterable한 대상)

위의 맵함수를 통해 반환되는 iterator 객체는 list나 tuple과 같은 sequence 자료형으로 변환 가능하다. 

def square(x):
    return x ** 2

lst = [1, 2, 3, 4, 5]
squared_lst = list(map(square, lst))
print(squared_lst) # 출력: [1, 4, 9, 16, 25]

 

 

판다스

unique()

df['칼럼명'] 

df.loc[label]

df.iloc[index]

 

-preprocess

df_can.drop(['AREA','REG','DEV','Type','Coverage'], axis=1, inplace=True)

df_can.rename(columns={"OdName":"Country", "AreaName":"continent", "RegName":"detail_continent"}, inplace=True)

df_can.set_index("Country", inplace=True)
df_can.index.name=None

df_can.columns=list(map(str, df_can.columns))
years=list(map(str, range(1980, 2014)))

condition=df_can['continent']=='Asia'

df_can[condition]

df_can =df_can[(df_can['continent']=='Asia') & (df_can['detail_continent']=='Eastern Asia')]

 

 

 

matplotlib

plt.title()

plit.ylabel()

plit.xlabel()

plt.text(x,y,text)

china = china.transpose()

df_can.sort_values(by='Total', ascending=False, axis=0, inplace=True)

 

<code>

# we are using the inline backend
%matplotlib inline 

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.style.use(['ggplot']) # optional: for ggplot-like style

 

china.index = china.index.map(int) # let's change the index values of Haiti to type integer for plotting
china.plot(kind='line')

plt.title('Immigration from Haiti')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

# annotate the 2010 Earthquake. 
# syntax: plt.text(x, y, label)
plt.text(2000, 6000, '2010 Earthquake') # see note below

plt.show()