IT

파이썬 series,dataframe

프로개발러 2023. 9. 20. 21:19
반응형
#!/usr/bin/env python
# coding: utf-8

# In[9]:


#기본 설정
import numpy as np #numpy는 행렬,다차원 배열 지원하는 파이썬의
import pandas as pd

import datetime 
from datetime import datetime,date

pd.set_option('display.notebook_repr_html',False)
pd.set_option('display.max_columns',8)
pd.set_option('display.max_rows',10)
pd.set_option('display.width',80)

import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')


# In[13]:


s = pd.Series([1,2,3,4]) #단변향 데이터

print(s)#index count 0부터 같이 뿌려줌
print(s[0])
print(s[1])


# In[14]:


#get value a label 1
s[1]


# In[17]:


#return a series with the row with labels 1 and 3
s[[1,3]]
#s[1,3] error 발생. 


# In[41]:


#index와 데이터를 같이 넣어줌
s = pd.Series([1,2,3,4],
              index=['a','b','c','d'])
s


# In[45]:


#index와 데이터를 같이 넣어줌
s = pd.Series([1,2,3,4],
              index=[1,2,3,4])
s


# In[35]:


#loc속성값 : loc는 무저건 인덱스임.
#주피터는 순서가 중요함.
s.loc[['a','d']]


# In[38]:


#순서가 중요함. 
s.loc[[1,2]]


# In[43]:


#대괄호 연산자는 제로베이스 연산을 포함함.
#s[['a','b']] 랑 같은 결과
s[[1,2]]


# In[46]:


s.index


# In[50]:


#pandas는 시계열 데이터에 특화됨.
#date_range 시작과 종료값에 따른 인덱스를 만들어줌.
dates = pd.date_range('2016-04-01','2016-04-06')
dates


# In[53]:


temps1 = pd.Series([80,82,85,90,83,87],
                  index=dates)
temps1


# In[55]:


#2016-04-04 인 인덱스의 값을 가져와라
temps1['2016-04-04']


# In[57]:


#zerobase position을 해도 인식됨
temps1[3]


# In[64]:


temps2 = pd.Series([70,75,69,83,79,77],
                  index=dates)
#동일한 index 날짜끼리의 연산이 가능함.
#동일한 인덱스끼리는 연산도 가능하다.
#index가 다를경우 연산하면 에러남.
temp_diffs = temps1-temps2
temp_diffs


# In[65]:


temp_diffs[2]


# In[66]:


#mean은 평균값
#mean은 형변환이 일어나면 정수인데도 실수로 표시됨.
temp_diffs.mean()


# In[67]:


#사전형식 {} key:value 형식
temps_df = pd.DataFrame(
            {'Missoula':temps1
            ,'Philadelphia':temps2})
temps_df


# In[68]:


#열의 이름을 찾아줌. 기본값은 열을 우선으로 찾아줌
temps_df['Missoula']


# In[70]:


temps_df['Philadelphia']


# In[71]:


A = temps_df[['Philadelphia','Missoula']]
A


# In[75]:


#컬럼 하나하나가 하나의 속성역할.
#컬럼명이 이상있으면 오류를 보낼수 있음.
temps_df.Missoula


# In[77]:


temps_df.Missoula - temps_df.Philadelphia


# In[79]:


#Difference 키값을 새로 만듬. 기존에 있으면 대체됨.
temps_df['Difference'] = temp_diffs
temps_df


# In[80]:


#모든 컬럼을 가져옴
temps_df.columns


# In[82]:


#위치값을 가져옴. 연속적인 데이터를 가져올때는 slicing을 사용함. 초깃값:종료값-1 
#열과 행이 모두 들어감.
temps_df.Difference[1:4]


# In[83]:


#loc -  index 레이블을 찾음 . iloc ? zero base로만 찾음
#열 피봇이 일어남. 한줄의 결과값이 행으로 들어옴
temps_df.iloc[1]


# In[84]:


temps_df.iloc[1].index


# In[85]:


#열과 행이  cross된 데이터를 찾아줌
#데이터프레임이라서 행과 열을 찾아줌
temps_df.loc['2016-04-01','Philadelphia']


# In[ ]:


temps_df.loc['2016-04-01','Philadelphia']

#dataframe이니 열에 대한 개념이 추가됨.
temps_df.iloc[[1,3,5]].Difference
반응형