매일 공식문서 읽어보기 003 - Pandas 1.5.0

김영하·2022년 10월 12일
0

Viewing data

데이터 보기

See the Basics section.

핵심 기본 기능 (Essential basic functionality) 주제를 참고합니다.

Use DataFrame.head() and DataFrame.tail() to view the top and bottom rows of the frame respectively:

데이터프레임의 앞부분, 뒷부분을 보기 위해 각각 DataFrame.head()DataFrame.tail()을 사용합니다:

In [13]: df.head()
Out[13]: 
                   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

In [14]: df.tail(3)
Out[14]: 
                   A         B         C         D
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

Display the DataFrame.index or DataFrame.columns:

데이터프레임의 인덱스를 보려면 DataFrame.index를, 데이터프레임의 컬럼이름들을 보려면 DataFrame.columns를 사용합니다:

In [15]: df.index
Out[15]: 
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 [16]: df.columns
Out[16]: Index(['A', 'B', 'C', 'D'], dtype='object')

DataFrame.to_numpy() gives a NumPy representation of the underlying data. Note that this can be an expensive operation when your DataFrame has columns with different data types, which comes down to a fundamental difference between pandas and NumPy: NumPy arrays have one dtype for the entire array, while pandas DataFrames have one dtype per column. When you call DataFrame.to_numpy(), pandas will find the NumPy dtype that can hold all of the dtypes in the DataFrame. This may end up being object, which requires casting every value to a Python object.

DataFrame.to_numpy()을 사용하면 데이터를 NumPy 배열 형태로 변환합니다. 데이터프레임을 이루는 컬럼들이 서로 다른 자료형을 가지면 처리가 많이 필요하며, 이는 판다스와 NumPy간의 근본적인 차이때문입니다. NumPy 배열은 전체 배열 데이터에 대해서 1개의 자료형을 가지는 반면, 판다스 데이터프레임은 컬럼당 1개의 자료형을 가지고 있기 때문입니다. DataFrame.to_numpy()를 호출할 때, 판다스는 데이터프레임의 모든 자료형을 다룰 수 있는 NumPy 자료형을 찾습니다. 결국은 객체가 되어서 모든 값이 파이썬 객체로 전환 (cast)될 필요가 있습니다.

For df, our DataFrame of all floating-point values, and DataFrame.to_numpy() is fast and doesn’t require copying data:

아래 예제의 데이터프레임 df의 데이터는 모두 소수형 값을 가지고 있습니다. DataFrame.to_numpy() 연산은 빠르고, 별도로 데이터를 복사할 필요가 없습니다:

In [17]: df.to_numpy()
Out[17]: 
array([[ 0.4691, -0.2829, -1.5091, -1.1356],
       [ 1.2121, -0.1732,  0.1192, -1.0442],
       [-0.8618, -2.1046, -0.4949,  1.0718],
       [ 0.7216, -0.7068, -1.0396,  0.2719],
       [-0.425 ,  0.567 ,  0.2762, -1.0874],
       [-0.6737,  0.1136, -1.4784,  0.525 ]])

For df2, the DataFrame with multiple dtypes, DataFrame.to_numpy() is relatively expensive:

아래 예제의 데이터프레임 df2는 다양한 자료형을 가지고 있으며, DataFrame.to_numpy()은 비교적으로 처리가 많이 필요합니다:

In [18]: df2.to_numpy()
Out[18]: 
array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']],
      dtype=object)

Note
DataFrame.to_numpy() does not include the index or column labels in the output.

참고
DataFrame.to_numpy()는 인덱스 및 컬럼 이름을 출력할 때, 보여주지 않습니다.

describe() shows a quick statistic summary of your data:

describe()는 데이터의 기술통계량을 간단히 보여줍니다:

In [19]: df.describe()
Out[19]: 
              A         B         C         D
count  6.000000  6.000000  6.000000  6.000000
mean   0.073711 -0.431125 -0.687758 -0.233103
std    0.843157  0.922818  0.779887  0.973118
min   -0.861849 -2.104569 -1.509059 -1.135632
25%   -0.611510 -0.600794 -1.368714 -1.076610
50%    0.022070 -0.228039 -0.767252 -0.386188
75%    0.658444  0.041933 -0.034326  0.461706
max    1.212112  0.567020  0.276232  1.071804

Transposing your data:

행과 열을 서로 교환할 수 있습니다.

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

DataFrame.sort_index() sorts by an axis:

DataFrame.sort_index()를 사용해서 축을 기준으로 정렬할 수 있습니다:

In [21]: df.sort_index(axis=1, ascending=False)
Out[21]: 
                   D         C         B         A
2013-01-01 -1.135632 -1.509059 -0.282863  0.469112
2013-01-02 -1.044236  0.119209 -0.173215  1.212112
2013-01-03  1.071804 -0.494929 -2.104569 -0.861849
2013-01-04  0.271860 -1.039575 -0.706771  0.721555
2013-01-05 -1.087401  0.276232  0.567020 -0.424972
2013-01-06  0.524988 -1.478427  0.113648 -0.673690

DataFrame.sort_values() sorts by values:

DataFrame.sort_values()를 사용해서 값을 기준으로 정렬할 수 있습니다:

In [22]: df.sort_values(by="B")
Out[22]: 
                   A         B         C         D
2013-01-03 -0.861849 -2.104569 -0.494929  1.071804
2013-01-04  0.721555 -0.706771 -1.039575  0.271860
2013-01-01  0.469112 -0.282863 -1.509059 -1.135632
2013-01-02  1.212112 -0.173215  0.119209 -1.044236
2013-01-06 -0.673690  0.113648 -1.478427  0.524988
2013-01-05 -0.424972  0.567020  0.276232 -1.087401
profile
항상 공부하고 나누고 싶은 낭만학습자

0개의 댓글