인공지능

데이터분석 - Pandas

멋인러닝 2023. 3. 19. 23:34

데이터분석 강의를 듣고

여러가지 복습을 위해 

다시금 정리하면서

되새겨볼려한다.

처음에 아나콘다를 깔고

파이선을 깔고

벌써 4년전에 했던 작업인데

 

다시 내가 하고 있을 줄이야

그동안의 시간을 허투루 보내진 않았지만

이제는 다시 정말

끝까지 실력을 

어나더 레벨로 올리고

하루를 보내고 싶다.

 

기초가 튼튼해야 

무너지지않으리

기본부터 다시 보자.

 

판다스시작 안내 홈페이지이다.

https://pandas.pydata.org/getting_started.html

 

pandas - Python Data Analysis Library

Getting started Installation instructions The next steps provides the easiest and recommended way to set up your environment to use pandas. Other installation options can be found in the advanced installation page. Download Anaconda for your operating syst

pandas.pydata.org

시작하기 앞서 아나콘다를 설치한다.

https://www.anaconda.com/products/distribution

 

Anaconda | Anaconda Distribution

Anaconda's open-source Distribution is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

아나콘다를 깔면

주피터 노트북에서 새로운 파일을 생성후에

다음과 같이 import를 해준다.

 

In [1]: import numpy as np
In [2]: import pandas as pd

먼저 나오는 개념이 시리즈!

https://pandas.pydata.org/docs/user_guide/10min.html

 

10 minutes to pandas — pandas 1.5.3 documentation

10 minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can see more complex recipes in the Cookbook. Customarily, we import as follows: In [1]: import numpy as np In [2]: import pandas as pd Object creation See the In

pandas.pydata.org

자세한건 홈피에 있지만

나는 이해한걸 정리하는 느낌!

 

In [3]: s = pd.Series([1, 3, 5, np.nan, 6, 8])

In [4]: s
Out[4]: 
0    1.0
1    3.0
2    5.0
3    NaN
4    6.0
5    8.0
dtype: float64

먼저 객체를 생성하고 아웃풋을 출력해보았다.

 

두번째 데이터프레임 만들기

DataFrame열을 사용하고 레이블이 지정된 날짜 시간 인덱스가 있는 NumPy 배열을 전달하여 만들기 datae_range():

 

In [5]: dates = pd.date_range("20130101", periods=6)

In [6]: dates
Out[6]: 
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')

In [7]: df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD"))

In [8]: df
Out[8]: 
                   A         B         C         D
2013-01-01  0.469112 -0.282863 -1.509059 -1.135632
2013-01-02  1.212112 -0.173215  0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929  1.071804
2013-01-04  0.721555 -0.706771 -1.039575  0.271860
2013-01-05 -0.424972  0.567020  0.276232 -1.087401
2013-01-06 -0.673690  0.113648 -1.478427  0.524988

dates라는 객체를 만들었다. 

배열  data_range를 써서. 6개

그 다음 DataFrame 으로 6, 4 데이터 프레임 만듬.

 

DataFrame 시리즈와 유사한 구조로 변환할 수 있는 객체 사전을 전달하여 생성 

In [9]: df2 = pd.DataFrame(
   ...:     {
   ...:         "A": 1.0,
   ...:         "B": pd.Timestamp("20130102"),
   ...:         "C": pd.Series(1, index=list(range(4)), dtype="float32"),
   ...:         "D": np.array([3] * 4, dtype="int32"),
   ...:         "E": pd.Categorical(["test", "train", "test", "train"]),
   ...:         "F": "foo",
   ...:     }
   ...: )
   ...: 

In [10]: df2
Out[10]: 
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo

 

시작이 반이다. 

조금씩 이라도 해보자.

숙달 될  때 깢이...