Python 数据帧中列数组的行值
我有一个熊猫数据框,看起来像这样:Python 数据帧中列数组的行值,python,pandas,scikit-learn,Python,Pandas,Scikit Learn,我有一个熊猫数据框,看起来像这样: +---+--------+-------------+------------------+ | | ItemID | Description | Feedback | +---+--------+-------------+------------------+ | 0 | 8988 | Tall Chair | I hated it | +---+--------+-------------+------------
+---+--------+-------------+------------------+
| | ItemID | Description | Feedback |
+---+--------+-------------+------------------+
| 0 | 8988 | Tall Chair | I hated it |
+---+--------+-------------+------------------+
| 1 | 8988 | Tall Chair | Best chair ever |
+---+--------+-------------+------------------+
| 2 | 6547 | Big Pillow | Soft and amazing |
+---+--------+-------------+------------------+
| 3 | 6547 | Big Pillow | Horrific color |
+---+--------+-------------+------------------+
我想将“Feedback”列中的值连接到一个新列中,用逗号分隔,ItemID在其中匹配。像这样:
+---+--------+-------------+----------------------------------+
| | ItemID | Description | NewColumn |
+---+--------+-------------+----------------------------------+
| 0 | 8988 | Tall Chair | I hated it, Best chair ever |
+---+--------+-------------+----------------------------------+
| 1 | 6547 | Big Pillow | Soft and amazing, Horrific color |
+---+--------+-------------+----------------------------------+
我尝试了几种不同的透视、合并、堆叠等方法,但都被卡住了。我认为NewColumn最终将成为一个数组,但我对Python还相当陌生,所以我不确定。
此外,最终,我将尝试将其用于文本分类(对于新的“描述”生成一些“反馈”标签[多类问题])调用数据帧上的
.groupby('ItemID')
,然后连接反馈列:
df.groupby('ItemID')['Feedback'].apply(lambda x: ', '.join(x))
请参阅。我认为您可以按列ItemID
和Description
,join
和last:
如果不需要说明
列:
print df.groupby(['ItemID'])['Feedback'].apply(', '.join).reset_index(name='NewColumn')
ItemID NewColumn
0 6547 Soft and amazing, Horrific color
1 8988 I hated it, Best chair ever
print df.groupby(['ItemID'])['Feedback'].apply(', '.join).reset_index(name='NewColumn')
ItemID NewColumn
0 6547 Soft and amazing, Horrific color
1 8988 I hated it, Best chair ever