Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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 熊猫未熔化数据集_Pandas_Pivot Table - Fatal编程技术网

Pandas 熊猫未熔化数据集

Pandas 熊猫未熔化数据集,pandas,pivot-table,Pandas,Pivot Table,我有一个这样的数据集 CookieID ItemID 0 ERG-278-REDD 5651 1 NaN 2377 2 STQ-134-DDVH 1217 3 NaN 1798 4 XYZ-541-EFFG 1234 5 NaN 2378 我想把它转换成熊猫 CookieID Item1 Item2 ERG-278-RREDD 5651

我有一个这样的数据集

    CookieID      ItemID
0   ERG-278-REDD    5651
1   NaN             2377
2   STQ-134-DDVH    1217
3   NaN             1798
4   XYZ-541-EFFG    1234
5   NaN             2378
我想把它转换成熊猫

CookieID          Item1 Item2
ERG-278-RREDD     5651  2377
STQ-134-DDVH      1217  1798
XYZ-541-EFFG      1234  2378
我尝试使用pivot表,但它不起作用。这是我的透视表命令

dfunmelt = pd.pivot_table(dfmelt, index=['CookieID'],columns='ItemID',aggfunc=len)
如何实现上述输出?

  • 设置索引
    +
    ffill
  • groupby
    +
    cumcount
  • 重新分配
    索引
  • 取消堆叠
    +
    重命名

  • 设置索引
    +
    ffill
  • groupby
    +
    cumcount
  • 重新分配
    索引
  • 取消堆叠
    +
    重命名


这里有一个使用
pivot\u表的单行程序

In [371]: (df.assign(no = df['CookieID']isnull().astype(int))
             .ffill()
             .pivot_table(index='CookieID', values='ItemID', columns='no', aggfunc='sum')
             .rename(columns='Item_{}'.format))
Out[371]:
no            Item_0  Item_1
CookieID
ERG-278-REDD    5651    2377
STQ-134-DDVH    1217    1798
XYZ-541-EFFG    1234    2378
详细信息

assign
NaN
值创建一个新列

In [372]: df.assign(no = df.CookieID.isnull().astype(int))
Out[372]:
       CookieID  ItemID  no
0  ERG-278-REDD    5651   0
1           NaN    2377   1
2  STQ-134-DDVH    1217   0
3           NaN    1798   1
4  XYZ-541-EFFG    1234   0
5           NaN    2378   1
然后使用
ffill
填充
NaN

In [373]: df.assign(no = df.CookieID.isnull().astype(int)).ffill()
Out[373]:
       CookieID  ItemID  no
0  ERG-278-REDD    5651   0
1  ERG-278-REDD    2377   1
2  STQ-134-DDVH    1217   0
3  STQ-134-DDVH    1798   1
4  XYZ-541-EFFG    1234   0
5  XYZ-541-EFFG    2378   1
然后,您可以按原样使用透视表

In [374]: df.assign(no = df.CookieID.isnull().astype(int)).ffill().pivot_table(ind
     ...: ex='CookieID', values='ItemID', columns='no', aggfunc='sum')
Out[374]:
no               0     1
CookieID
ERG-278-REDD  5651  2377
STQ-134-DDVH  1217  1798
XYZ-541-EFFG  1234  2378

使用
rename()

In [371]: (df.assign(no = df['CookieID']isnull().astype(int))
             .ffill()
             .pivot_table(index='CookieID', values='ItemID', columns='no', aggfunc='sum')
             .rename(columns='Item_{}'.format))
Out[371]:
no            Item_0  Item_1
CookieID
ERG-278-REDD    5651    2377
STQ-134-DDVH    1217    1798
XYZ-541-EFFG    1234    2378
详细信息

assign
NaN
值创建一个新列

In [372]: df.assign(no = df.CookieID.isnull().astype(int))
Out[372]:
       CookieID  ItemID  no
0  ERG-278-REDD    5651   0
1           NaN    2377   1
2  STQ-134-DDVH    1217   0
3           NaN    1798   1
4  XYZ-541-EFFG    1234   0
5           NaN    2378   1
然后使用
ffill
填充
NaN

In [373]: df.assign(no = df.CookieID.isnull().astype(int)).ffill()
Out[373]:
       CookieID  ItemID  no
0  ERG-278-REDD    5651   0
1  ERG-278-REDD    2377   1
2  STQ-134-DDVH    1217   0
3  STQ-134-DDVH    1798   1
4  XYZ-541-EFFG    1234   0
5  XYZ-541-EFFG    2378   1
然后,您可以按原样使用透视表

In [374]: df.assign(no = df.CookieID.isnull().astype(int)).ffill().pivot_table(ind
     ...: ex='CookieID', values='ItemID', columns='no', aggfunc='sum')
Out[374]:
no               0     1
CookieID
ERG-278-REDD  5651  2377
STQ-134-DDVH  1217  1798
XYZ-541-EFFG  1234  2378

使用
rename()
获取您的列名。

如果我每个cookie有2个以上的项目,这是否有效?如果我每个cookie有2个以上的项目,这是否有效?