如何为分组依据结果设置列名

时间:2020-09-16 18:25:38

标签: python pandas group-by pandas-groupby

我有一个大df。在两列上使用groupby时,在第三列上有一个value_counts。到目前为止一切顺利:

df_group_chir = df_chir.groupby(['Spec','Year'])['type'].value_counts()

这给出了预期的结果:

                                        type
Spec          year       type
CHI          2017          D             876
                           K             639
             2018          D             893
                           K             524
             2019          D             969
                           K             567

名称:类型,dtype:int64

但是.....您会看到它说:名称:类型。没有其他的。以下代码也显示了第一列:

df_group_chir.columns
Index(['type'], dtype='object')

第一个结果当然是熊猫系列。使该系列成为DF,当然会得到相同的结果。

知道以下内容是行不通的,但还是尝试了一下:

df_group_chir.columns=['Spec', 'Year', 'Type', 'Total']

结果:

ValueError: Length mismatch: Expected axis has 1 elements, new values have 4 elements

我的明显问题:如何在groupby结果中获取(四个)列名?

提前谢谢!!问候Jan

1 个答案:

答案 0 :(得分:3)

让我们尝试使用sum_setT选项的datatype Trie = Trie(letters: map<char, Trie>, word: bool) function size(t: Trie): nat decreases t { 1 + sum_setT(t, t.letters.Values) } function sum_setT(t: Trie, xs: set<Trie>): nat requires xs <= t.letters.Values decreases t, xs { if |xs| == 0 then 0 else var x :| x in xs; var sofar := sum_setT(t, xs - {x}); sofar + size(x) }

public Transform obstacle;
public GameObject Obstacle;
public Text Score;
public int counter;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (obstacle.transform.position.z <= - 3)
    {
        Destroy(Obstacle);
        counter = counter + 1;
    }
    Score.text = counter.ToString();
}
相关问题