Python 打印时对数据框列进行排序

Python 打印时对数据框列进行排序,python,pandas,matplotlib,histogram,Python,Pandas,Matplotlib,Histogram,我有一个包含多列的熊猫数据框架,如下所示: columns_all = pd.DataFrame({'m1_h':m1_hist, 'm2_h':m2_hist, ....... 'm6_h':m6_hist, 'm6_f':m6_futu}) 我使用下面的方法根据每一列绘制直方图,但是列是被排序的,但是我希望所有直方图的顺序与上面数据框中所写的列相同: columns_all.hist(layout=(2,6), sharey=True, sharex=True) plt.ylim(0, 1

我有一个包含多列的熊猫数据框架,如下所示:

columns_all = pd.DataFrame({'m1_h':m1_hist, 'm2_h':m2_hist, ....... 'm6_h':m6_hist, 'm6_f':m6_futu})
我使用下面的方法根据每一列绘制直方图,但是列是被排序的,但是我希望所有直方图的顺序与上面数据框中所写的列相同:

columns_all.hist(layout=(2,6), sharey=True, sharex=True)
plt.ylim(0, 100)
plt.xlim(0, 150)
plt.show()

如有建议,请在绘图时保持列的顺序

您可以重复调用各个列,因为在创建dataframe和.hist()时都会自动进行重新排序:

s = pd.DataFrame([{'B': 1.5, 'A':3, 'C': 4, 'D':2}])
s

    A   B   C   D
0   3   1.5 4   2

s = s[["B", "A", "C", "D"]] #chose your order
s

    B   A   C   D
0   1.5 3   4   2

for x in s.columns:
    s[[x]].hist(layout=(2,6), sharey=True, sharex=True)
plt.ylim(0, 100)
plt.xlim(0, 150)
plt.show()

您可以重复调用各个列,因为在创建dataframe和.hist()时,会自动进行重新排序:

s = pd.DataFrame([{'B': 1.5, 'A':3, 'C': 4, 'D':2}])
s

    A   B   C   D
0   3   1.5 4   2

s = s[["B", "A", "C", "D"]] #chose your order
s

    B   A   C   D
0   1.5 3   4   2

for x in s.columns:
    s[[x]].hist(layout=(2,6), sharey=True, sharex=True)
plt.ylim(0, 100)
plt.xlim(0, 150)
plt.show()
根据,排序由
\u try\u sort(data.columns)
定义,不能由参数更改。你能做什么。然而,在我的测试中,这不会给你一个
(2,6)
布局。如果您确实需要该布局以及pandas.DataFrame.hist的功能,以下代码可能会有所帮助:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

columns_all = pd.DataFrame([np.random.randn(1000)] * 7).T
columns_all.columns = ['m1_h', 'm2_h', 'm3_h', 'm4_h', 'm5_h', 'm6_h', 'm6_f']
plt.clf()
fig = plt.figure(figsize=(16, 4))
axarr = []
for i, col in enumerate(columns_all.columns):
    if i // 6 > 0:
        sharex = axarr[i % 6]
        plt.setp(axarr[i % 6].get_xticklabels(), visible=False)
    else:
        sharex = None
    if i % 6 > 0:
        sharey = axarr[i // 6]
    else:
        sharey = None
    ax = fig.add_subplot(2, 6, i + 1, sharex=sharex, sharey=sharey)
    axarr.append(ax)
    if i % 6 > 0:
        plt.setp(ax.get_yticklabels(), visible=False)
    ax.hist(columns_all[col].dropna().values)
    ax.set_title(col)
    ax.grid(True)
fig.subplots_adjust(wspace=0.3, hspace=0.3)
plt.show()

根据,排序由
\u try\u sort(data.columns)
定义,不能由参数更改。你能做什么。然而,在我的测试中,这不会给你一个
(2,6)
布局。如果您确实需要该布局以及pandas.DataFrame.hist的功能,以下代码可能会有所帮助:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

columns_all = pd.DataFrame([np.random.randn(1000)] * 7).T
columns_all.columns = ['m1_h', 'm2_h', 'm3_h', 'm4_h', 'm5_h', 'm6_h', 'm6_f']
plt.clf()
fig = plt.figure(figsize=(16, 4))
axarr = []
for i, col in enumerate(columns_all.columns):
    if i // 6 > 0:
        sharex = axarr[i % 6]
        plt.setp(axarr[i % 6].get_xticklabels(), visible=False)
    else:
        sharex = None
    if i % 6 > 0:
        sharey = axarr[i // 6]
    else:
        sharey = None
    ax = fig.add_subplot(2, 6, i + 1, sharex=sharex, sharey=sharey)
    axarr.append(ax)
    if i % 6 > 0:
        plt.setp(ax.get_yticklabels(), visible=False)
    ax.hist(columns_all[col].dropna().values)
    ax.set_title(col)
    ax.grid(True)
fig.subplots_adjust(wspace=0.3, hspace=0.3)
plt.show()