Python 将分类行转换为具有字符串值的列标题(无聚合)

Python 将分类行转换为具有字符串值的列标题(无聚合),python,pivot,reshape,transpose,Python,Pivot,Reshape,Transpose,我想将分类行转换为列,而不进行任何聚合。我用pivot试过了,但我得了NANs 这是数据帧: data = pd.DataFrame({'Art':['blue', 'red', 'blue', 'red', 'blue', 'red', 'blue', 'red'], 'Description':['Some text 1', 'Some text 2', 'Some text 3', 'Some text 4', 'Some text 5', 'Some text 6', 'Some te

我想将分类行转换为列,而不进行任何聚合。我用pivot试过了,但我得了NANs

这是数据帧:

data = pd.DataFrame({'Art':['blue', 'red', 'blue', 'red', 'blue', 'red', 'blue', 'red'],

'Description':['Some text 1', 'Some text 2', 'Some text 3', 'Some text 4', 'Some text 5', 'Some text 6', 'Some text 7', 'Some text 8']})
当我尝试旋转时:

data.pivot(columns='Art')

我得到:

我已经这样解决了NAN问题:

data.pivot(columns='Art').apply(lambda x:pd.Series(x.dropna().values))

这是理想的结果:

然而,我想知道是否有一种更聪明的方法可以简单地将我的类作为列标题


谢谢大家!

您的解决方案看起来已经相当清晰简洁了。。只需一行代码,即可得到结果:

data.pivot(columns='Art').apply(lambda x: pd.Series(x.dropna().values))