Pandas: 닀쀑 ν–‰ μ„œλΈŒν”Œλ‘―μ„ ν”Œλ‘―ν•  수 μ—†μŒ

에 λ§Œλ“  2014λ…„ 08μ›” 26일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: pandas-dev/pandas

이 μ½”λ“œλŠ” 3개의 μ„œλΈŒν”Œλ‘―μœΌλ‘œ κ΅¬μ„±λœ 단일 행을 μƒμ„±ν•˜λŠ” 데 잘 μž‘λ™ν•©λ‹ˆλ‹€.

g = np.random.choice([1,2,3], 10)
s = np.random.normal(size=10)
s2 =np.random.normal(size=10)
df = pd.DataFrame([g, s, s2]).T
df.columns = ['key', 's1', 's2']
gb = df.groupby('key')

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 4))
i = 0
for key, df2 in gb:
    df2.plot(ax=axes[i], x='s1', y='s2', title=key)
    i = i + 1

κ·ΈλŸ¬λ‚˜ 두 번째 ν–‰(nrows=2)을 μΆ”κ°€ν•˜λ €κ³  ν•˜λ©΄ 속성 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

AttributeError: 'numpy.ndarray' κ°œμ²΄μ— 'get_figure' 속성이 μ—†μŠ΅λ‹ˆλ‹€.

fig, axes = plt.subplots(nrows=2, ncols=3)
fig.tight_layout() # Or equivalently,  "plt.tight_layout()"
i = 1
for key, df2 in gb:
    df2.plot(ax=axes[i])
    i = i + 1

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

두 번째 ν–‰μœΌλ‘œ λ‘˜λŸ¬μ‹Έκ³  μ‹Άλ‹€λ©΄ axes[i // 3][i % 3] 와 같이 ν•΄μ•Ό ν•©λ‹ˆλ‹€.
(예λ₯Ό 6개의 그룹으둜 ν™•μž₯ν–ˆμŠ΅λ‹ˆλ‹€)

In [67]: df
Out[67]: 
    key        s1        s2
0     3 -1.452043 -0.119374
1     1  0.603860 -1.635034
2     3  0.964165 -0.043124
3     2  0.459628 -0.538155
4     3  0.398761 -0.195261
5     1  0.085750 -0.116766
6     2 -0.397419 -0.140660
7     3 -0.053209  1.547755
8     1 -0.634555 -0.509077
9     3  0.138808  0.608165
10    6 -1.452043 -0.119374
11    4  0.603860 -1.635034
12    6  0.964165 -0.043124
13    5  0.459628 -0.538155
14    6  0.398761 -0.195261
15    4  0.085750 -0.116766
16    5 -0.397419 -0.140660
17    6 -0.053209  1.547755
18    4 -0.634555 -0.509077
19    6  0.138808  0.608165

In [63]: for i, (key, df2) in enumerate(gb):
    df2.plot(ax=axes[i // 3][i % 3])

ex

λͺ¨λ“  3 λŒ“κΈ€

axes κ°€ 이제 matplotlib μΆ•μ˜ 2차원 배열이기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

In [35]: fig, axes = plt.subplots(nrows=2, ncols=3)

In [36]: axes
Out[36]: 
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10c094ac8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10ee40b70>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10c0ac240>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x10edf8a90>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10ec27630>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10edc9128>]], dtype=object)

In [37]: axes.shape
Out[37]: (2, 3)

λ‹€μŒκ³Ό 같은 것을 μ‹œλ„ν•˜μ‹­μ‹œμ˜€.

In [38]: for i, (key, df2) in enumerate(gb):
    df2.plot(ax=axes[0][i])

두 번째 ν–‰μœΌλ‘œ λ‘˜λŸ¬μ‹Έκ³  μ‹Άλ‹€λ©΄ axes[i // 3][i % 3] 와 같이 ν•΄μ•Ό ν•©λ‹ˆλ‹€.
(예λ₯Ό 6개의 그룹으둜 ν™•μž₯ν–ˆμŠ΅λ‹ˆλ‹€)

In [67]: df
Out[67]: 
    key        s1        s2
0     3 -1.452043 -0.119374
1     1  0.603860 -1.635034
2     3  0.964165 -0.043124
3     2  0.459628 -0.538155
4     3  0.398761 -0.195261
5     1  0.085750 -0.116766
6     2 -0.397419 -0.140660
7     3 -0.053209  1.547755
8     1 -0.634555 -0.509077
9     3  0.138808  0.608165
10    6 -1.452043 -0.119374
11    4  0.603860 -1.635034
12    6  0.964165 -0.043124
13    5  0.459628 -0.538155
14    6  0.398761 -0.195261
15    4  0.085750 -0.116766
16    5 -0.397419 -0.140660
17    6 -0.053209  1.547755
18    4 -0.634555 -0.509077
19    6  0.138808  0.608165

In [63]: for i, (key, df2) in enumerate(gb):
    df2.plot(ax=axes[i // 3][i % 3])

ex

정말 κ³ λ§ˆμ›Œμš”, ν†°. 이제 μ™„λ²½ν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€. 도움이 λ˜λŠ” 닡변에 μ§„μ‹¬μœΌλ‘œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰