如何在按组计数后创建新列?

时间:2019-11-05 15:44:27

标签: pandas pandas-groupby

我试图用group by和count创建一个新列。 但是,它将引发错误“插入的列的索引与框架索引不兼容”

import pandas as pd

#read csv
df1 = pd.read_csv('hi.txt',sep = '\t')#provide name and sheet of excel file
a=df1.groupby(['c','t']).count()
df1['difference']=a
print(df1)

输入:

coun    id  cat
A   12  90
U   13  91


1 个答案:

答案 0 :(得分:1)

使用:

SoundPlayer player = new SoundPlayer();
public static void StartBattelMusic()
        {
            player.SoundLocation = "D:....BattelMusic.wav";
            player.Play();
        }


        public static void SwordHitSound()
        {
            player.SoundLocation = "D:....SwordHitSound.wav";
            player.Play();
        }

new_df=df.groupby(['category','country'],sort=False).country.count().to_frame('count').reset_index()
print(new_df)