Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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
Python 3.x 将行数据拆分为列_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 将行数据拆分为列

Python 3.x 将行数据拆分为列,python-3.x,pandas,Python 3.x,Pandas,我有一个数据帧,如下所示 ID Party Votes RS-24 D 31 RS-24 R 12 我想做的是按照下面的步骤将行数据拆分成一个新列,这样我就可以运行一些基本的计算 ID D_Votes R_Votes RS-24 31 12 有人知道我该怎么做吗 非常感谢您的帮助。set\u index和unstack df1=df.set_index(['ID','Party']).unstack().swaplevel(0,1,axis=1) df1.

我有一个数据帧,如下所示

ID  Party   Votes

RS-24   D   31

RS-24   R   12
我想做的是按照下面的步骤将行数据拆分成一个新列,这样我就可以运行一些基本的计算

ID  D_Votes R_Votes

RS-24   31  12
有人知道我该怎么做吗


非常感谢您的帮助。

set\u index
unstack

df1=df.set_index(['ID','Party']).unstack().swaplevel(0,1,axis=1)
df1.columns=df1.columns.map('_'.join)
df1
Out[253]: 
       D_Votes  R_Votes
ID                     
RS-24       31       12

这也是
pivot
的经典案例:
df.pivot(index='ID',columns='Party',values='voces')。添加后缀(“u voces”)