Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 向dataframe添加列_Pandas - Fatal编程技术网

Pandas 向dataframe添加列

Pandas 向dataframe添加列,pandas,Pandas,我有以下资料: count=a[(a.type='exact')&(a.score==1.0)].groupby('id').count()['type'] countbyid=pd.DataFrame(data=count) 这给了我以下信息: id类型 102116 578 1256 1211 126805 215 31560 12 329401 2310 49923 375 9691 2409 当我尝试countbyid=pd.DataFrame(data=count,

我有以下资料:

count=a[(a.type='exact')&(a.score==1.0)].groupby('id').count()['type']
countbyid=pd.DataFrame(data=count)

这给了我以下信息:

id类型
102116  578
1256    1211
126805  215
31560   12
329401  2310
49923   375
9691 2409

当我尝试
countbyid=pd.DataFrame(data=count,columns=['customer','department'])
时,不返回任何数据,只返回列名:


客户部门

使用给定的方法,插入已经有列名的数据框,当您在其中分配列时,不会分配这些列的值,因此您会收到空数据框

所以有两种方法来解决这个问题-

第一:

countbyid = pd.DataFrame(data = countbyid.values)
countbyid.columns = ['customer', 'department']
第二(如ScottBoston评论中所述):


使用给定的方法,插入已经有列名的数据帧,当您在其中分配列时,不会分配这些列的值,因此您会收到一个空数据帧

所以有两种方法来解决这个问题-

第一:

countbyid = pd.DataFrame(data = countbyid.values)
countbyid.columns = ['customer', 'department']
第二(如ScottBoston评论中所述):


基于您的代码,我假设
a
是一个数据帧,您只需要重置索引并重命名列。无需再次调用数据帧构造函数

a[(a.type == 'exact') & (a.score == 1.0)].groupby('id').type.count().reset_index()\
.rename({'id':'customer', 'type':'department'})

基于您的代码,我假设
a
是一个数据帧,您只需要重置索引并重命名列。无需再次调用数据帧构造函数

a[(a.type == 'exact') & (a.score == 1.0)].groupby('id').type.count().reset_index()\
.rename({'id':'customer', 'type':'department'})

我不确定您是否需要这里的数据帧构造函数,请打印计数以查看我不确定您是否需要这里的数据帧构造函数,请打印计数以查看see@meW答对了,谢谢你的帮助和对我为什么收到空数据框的澄清。@meW答对了,谢谢你的帮助和对我为什么收到空数据框的澄清。