코딩/Python
판다스 이해하기 - 분할, 더미변수, 문자형 날짜형 변환
정듀이
2020. 7. 16. 15:37
분할¶
In [25]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important;}</style>"))
In [10]:
import pandas as pd
emp = pd.read_csv("c:/data/emp3.csv")
emp
Out[10]:
In [11]:
count, bin_dividers = np.histogram(emp.sal,bins=3)
print(count)
print(bin_dividers) # 경계값 리스트
In [12]:
bin_names = ['저소득','중간소득','고소득']
emp['sal_divide'] = pd.cut(x=emp.sal,bins=bin_dividers,labels=bin_names)
emp
Out[12]:
더미변수¶
In [13]:
pd.get_dummies(emp.deptno)
Out[13]:
문자형을 날짜형으로 변환¶
In [14]:
df = pd.read_csv("c:/data/studyfile/stock-data.csv")
print(df.info())
df.head()
Out[14]:
In [15]:
df[['Date']] = pd.to_datetime(df.Date)
df.info()
In [16]:
df.Date.dt.year.tail()
Out[16]:
In [17]:
df.Date.dt.month.head()
Out[17]:
In [18]:
df.Date.dt.day.head()
Out[18]:
인덱스를 날짜형으로 만들기¶
In [19]:
df.set_index('Date',inplace=True)
df
Out[19]:
In [20]:
df.index
Out[20]:
In [21]:
df.index.year
Out[21]:
In [22]:
df.index.month
Out[22]:
In [23]:
df.index.day
Out[23]: