Pandas: dataframe의 각 열에 대한 히스토그램 만들기


다음 기본 구문을 사용하여 Pandas DataFrame의 각 열에 대한 히스토그램을 만들 수 있습니다.

 import pandas as pd
import matplotlib. pyplot as plt

#define number of subplots
fig, axis = plt. subplots (1, 3)

#create histogram for each column in DataFrame
df. hist (ax=axis)

이 특정 예에서는 subplots() 함수를 사용하여 DataFrame에 3개의 열이 있음을 지정한 다음 각 열에 대한 히스토그램을 만듭니다.

다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.

예: Pandas 히스토그램의 각 열에 대한 히스토그램 만들기

세 개의 열을 포함하는 다음과 같은 pandas DataFrame이 있다고 가정합니다.

 import pandas as pd
import numpy as np

#make this example reproducible
n.p. random . seeds (1)

#createDataFrame
df = pd. DataFrame ({' points ': np. random . normal (loc=20, scale=2, size=300),
                   ' assists ': np. random . normal (loc=14, scale=3, size=300),
                   ' rebounds ': np. random . normal (loc=12, scale=1, size=300)})

#view head of DataFrame
print ( df.head ())

      points assists rebounds
0 23.248691 20.197350 10.927036
1 18.776487 9.586529 12.495159
2 18.943656 11.509484 11.047938
3 17.854063 11.358267 11.481854
4 21.730815 13.162707 10.538596

다음 구문을 사용하여 DataFrame의 세 열 각각에 대한 히스토그램을 만들 수 있습니다.

 import matplotlib. pyplot as plt

#define format for subplots (1 row and 3 columns)
fig, axis = plt. subplots (1, 3)

#create histogram for each column in DataFrame
df. hist (ax=axis)

결과는 DataFrame의 각 열에 대한 히스토그램을 표시하는 행 1개와 열 3개로 구성된 그리드입니다.

원하는 경우 figsize 인수를 사용하여 히스토그램의 크기를 변경하고 edgecolorGrid 인수를 사용하여 히스토그램의 모양을 개선할 수 있습니다.

 import matplotlib. pyplot as plt 

#define format for subplots
fig, axis = plt. subplots (1, 3, figsize=(8,3))

#create histogram for each column in DataFrame
df. hist (ax=axis, edgecolor=' black ', grid= False ) 

pandas는 DataFrame의 각 열에 대한 히스토그램을 만듭니다.

히스토그램의 정확한 형식과 크기를 정의하려면 subplots() 함수의 인수를 자유롭게 사용해 보세요.

추가 리소스

다음 튜토리얼에서는 Pandas에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

Pandas 히스토그램에 사용되는 저장소 수를 변경하는 방법
Pandas 히스토그램에서 X축 범위를 변경하는 방법
Pandas에서 그룹별로 히스토그램을 그리는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다