筛选column1中所有唯一项目作为键,同时将Column2和Column3作为键:值作为嵌套数据框中的值,从常规数据框中筛选

2024-10-01 09:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望将常规数据帧转换为嵌套数据帧,然后最终将嵌套数据帧转换回字典。你知道吗

在Pandas中清理完我的数据集后,数据框中的数据集如下所示:

输入:df.head(5)

输出:

    reviewerName    title                               reviewerRatings
0   Charles         Harry Potter Book Seven News:...    3.0
1   Katherine       Harry Potter Boxed Set, Books...    5.0
2   Lora            Harry Potter and the Sorcerer...    5.0
3   Cait            Harry Potter and the Half-Blo...    5.0
4   Diane           Harry Potter and the Order of...    5.0

接下来,我查看了数据集中唯一审阅者名称的数量:

输入:len(df['reviewerName'].uqinue())

输出:66130

现在,我正试图找到一种方法来获取所有66130唯一的reviewerName,并将它们都指定为新嵌套数据框中的键,然后使用“title”和“reviewerRatings”作为另一层属性来指定值关键字:值嵌套的数据帧。你知道吗

当我试着看第一个唯一值显示了多少时,我发现:

输入:df[df['reviewerName'] == 'Charles G']

输出:

      reviewerName                               title   reviewerRatings
0          Charles    Harry Potter Book Seven News:...               3.0
19156      Charles    Harry Potter and the Half-Blo...               3.5
19156      Charles    Harry Potter and the Order of...               4.0

我希望操纵数据帧,使其看起来像这样的输出:

           title                                reviewerRatings
Charles    Harry Potter Book Seven News:...     3.0
           Harry Potter and the Half-Blo...     3.5
           Harry Potter and the Order of...     4.0
Katherine  Harry Potter Boxed Set, Books...     5.0
           Harry Potter and the Half-Blo...     2.5
           Harry Potter and the Order of...     5.0

我试图将这三列(reviewerName、title、reviewerRatings)中的每一列分开,然后将这些项目串联在一起,但没有发现以下情况:

输入:

p1 = df[['reviewerName']]
p2 = df[['title']]
p3 = df[['reviewerRatings']]
concatenated = pd.concat([p1,p2,p3], keys=list[p1.unqiue])
concatenated

输出:

AttributeError                            Traceback (most recent call last)
<ipython-input-106-5a6be8c1a3ba> in <module>()
----> 1 concatenated = pd.concat([p1,p2,p3], keys=list[p1.unqiue])
      2 concatenated

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4370             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4371                 return self[name]
-> 4372             return object.__getattribute__(self, name)
   4373 
   4374     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'unqiue'

我也查了熊猫的资料,但运气不好,不确定这里是否有人能查到这个。你知道吗

一旦所需的输出得到解决,我希望将嵌套的数据帧转换成嵌套的字典。你知道吗

谢谢!你知道吗


Tags: andthe数据nameselfdftitlep1