是否可以将Pandas数据框列标题放在页脚中?

时间:2019-02-07 03:43:41

标签: python arrays pandas numpy dataframe

我有一个numpy数组,为简单起见,它为空。 尺寸为8x12。我定义了行标题和列标题。

column = [_ for _ in 'ABCDEFGH']
row = list(range(12, 0, -1))
self.board = np.full((12, 8, '||')
dataframe = pd.DataFrame(self.board, index=row, columns=column)

它看起来像这样:

     A   B   C   D   E   F   G   H
12  ||  ||  ||  ||  ||  ||  ||  ||
11  ||  ||  ||  ||  ||  ||  ||  ||
10  ||  ||  ||  ||  ||  ||  ||  ||
9   ||  ||  ||  ||  ||  ||  ||  ||
8   ||  ||  ||  ||  ||  ||  ||  ||
7   ||  ||  ||  ||  ||  ||  ||  ||
6   ||  ||  ||  ||  ||  ||  ||  ||
5   ||  ||  ||  ||  ||  ||  ||  ||
4   ||  ||  ||  ||  ||  ||  ||  ||
3   ||  ||  ||  ||  ||  ||  ||  ||
2   ||  ||  ||  ||  ||  ||  ||  ||
1   ||  ||  ||  ||  ||  ||  ||  ||

我想知道是否可以将列放在底部而不是顶部吗?

3 个答案:

答案 0 :(得分:0)

如果您对import java.util.Scanner; public class RecursiveKnapsack { static double max(double a, double b) { if (a > b) return a; else return b; } public static double m (int i, double weightLimit, double [] w) { if(i <= 0 || weightLimit == 0) return 0; else if (w[i] > weightLimit) return m(i-1,weightLimit,w); else return max(m(i-1, weightLimit, w),w[i] + m(i-1, weightLimit-w[i], w)); } public static void main (String[]args) { Scanner input = new Scanner(System.in); //NumberofItems System.out.println("Enter the number of items: "); int i = input.nextInt(); //Weight of Items System.out.println("Enter the weights for each item: "); double[] w = new double[i]; //Inserting input in array for (int x=0; x < i; x++){ w[x] = input.nextDouble(); } //Limit System.out.println("Enter the weight limit for the bag: "); double weightLimit = input.nextDouble(); System.out.println("The maximum weight of the items placed in the bag is " + m(i,weightLimit,w)); } } 数据帧的等宽输出感兴趣,则可能要签出tabulate

虽然它不支持页脚,但它可以很好地与pandas数据帧一起使用。

pandas

答案 1 :(得分:0)

我一直在寻找相同问题的答案,这就是为什么我找到了您的问题。
我知道此回复要晚25天,但这是我对StackOverflow(或与此有关的任何网站)的第一个回复,所以我还是这样做。
另外,也许将来会有其他人需要答案。我希望这有帮助。

我是新手,所以很高兴收到关于我的回应的指针/批评(无论大小)。

为了重新创建df模型,我必须在代码中修复一些括号,并且
摆脱“自我”作为变量b / c的一部分,这对我产生了错误。 另外,我添加了sort_index()来根据需要反向索引。

    import numpy as np
    import pandas as pd

    column = [_ for _ in 'ABCDEFGH']
    row = range(12, 0, -1)
    # board = np.full((12, 8), 7) # to test dtype changes
    board = np.full((12, 8), '||')
    df0 = pd.DataFrame(board, index=row, 
    columns=column).sort_index(ascending=False)

手动创建字典,其中键=列。值和值=相同名称的字符串。

    footer = {'A':'A', 'B':'B', 'C':'C', 'D':'D', 'E':'E', 'F':'F', 'G':'G', 'H':'H'}

或者让python为您做

    keys = list(df0.columns.values)
    values = list(df0.columns.values)
    footer = dict(zip(keys, values))

在df0后面加上字典。

    df1 = df0.append(footer, ignore_index=True)

有页脚但没有反向索引。
错误“如果ignore_index = True或系列具有名称,则只能追加系列”,这就是为什么索引不被反转的原因。 因此,这对于不需要反向索引的其他人可能有用。 这是显示棒球数据的页眉和页脚所需要的。

    df2 = df0.append(pd.Series((pd.Series(footer)), name='Footer'))  # Change 'Footer' to any text

必须将页脚放入嵌套的pd.Series()并添加名称以纠正上述错误。
没有嵌套,您将收到以下错误:“ append()得到了意外的关键字参数'name'”

    df3 = df0.append(pd.Series((pd.Series(footer)), name=''))  # Same as df2 but blank name

df0与您的模型数据帧匹配。 df1是无标题页脚但索引升序的解决方案。 df2是带有标题页脚和正确索引的解决方案。 df3是无标题页脚和正确索引的解决方案。

以上所有内容都包含页眉和页脚,OP只需要页脚。因此,我在下面找到了此以完成任务: Pandas - delete column name

@jezrael给出了这条简洁的代码来摆脱页眉(但要注意安全性-最好同时保留页眉和页脚。)

    df4 = df3
    df4.columns = [''] * len(df4.columns)

    print(df4)  # OP's solution

将一系列字符串添加到现有DF列会将所有dtypes转换为对象。

我认为,如果您需要对列进行计算,那么这里的解决方法是对df0的列进行计算,而不是 df4的文件,将它们存储在所需的位置,然后将其放入df4,以显示带有新数据的df4。

我希望有帮助。

答案 2 :(得分:0)

好吧,这有点小技巧,但出于显示目的,这是一种以保持宽度的方式将列名放在输出底部的非常快速的方法:

dataframe = dataframe.append(pd.Series(name='col'))
dataframe.iloc[ len(dataframe) - 1 ] = dataframe.columns

显然,最后一行仅用于显示目的:

      A   B   C   D   E   F   G   H
12   ||  ||  ||  ||  ||  ||  ||  ||
11   ||  ||  ||  ||  ||  ||  ||  ||
10   ||  ||  ||  ||  ||  ||  ||  ||
9    ||  ||  ||  ||  ||  ||  ||  ||
8    ||  ||  ||  ||  ||  ||  ||  ||
7    ||  ||  ||  ||  ||  ||  ||  ||
6    ||  ||  ||  ||  ||  ||  ||  ||
5    ||  ||  ||  ||  ||  ||  ||  ||
4    ||  ||  ||  ||  ||  ||  ||  ||
3    ||  ||  ||  ||  ||  ||  ||  ||
2    ||  ||  ||  ||  ||  ||  ||  ||
1    ||  ||  ||  ||  ||  ||  ||  ||
col   A   B   C   D   E   F   G   H