【问题标题】:Appending a list of values in a dataframe to a new column [duplicate]将数据框中的值列表附加到新列[重复]
【发布时间】:2020-09-12 02:30:57
【问题描述】:

我有一个包含如下推文的数据框:

我要做的是从列“in_reply_to_user_id”(不在图片中,因为 df 太宽而无法容纳)与给定 id 具有相同值的行中获取文本,并将文本附加到然后我想把它放在一个新列中。例如,列“in_reply_to_user_id”等于第一条推文的“id”的所有推文中的文本应该放在一个列表中,然后将其附加到数据框中称为“回复”的新列中。以下是我尝试过的一些事情:

for i in testb['in_reply_to_user_id']:
   for j in test['user.id']:
       if i == j:
           index=testb.index()
           test['replies'].append(testb['text'].iloc[index]) ```

test would be the original dataframe and testb would be a copy that i created in order to try to run the code above. it is just a copy of test.

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这是一个简单的解决方案,遍历所有行。

    import numpy as np
    import pandas as pd
    
    # example data
    df = pd.DataFrame({'id': [1, 2, 3, 4],
                       'text': ['How are you?', 'Fine.', 'Okay.', 'hi'], 
                       'in_reply_to_user_id': [4, 1, 1, 3]})
    
    # initiate new column
    df['replies'] = np.repeat(None, len(df))
    
    # assign lists as described in the question
    for i in df.index:
        df.at[i, 'replies'] = list(df.text[df.in_reply_to_user_id == df.id[i]])
    
    # show results
    df
    
        id  text            in_reply_to_user_id     replies
    0   1   How are you?    4                       [Fine., Okay.]
    1   2   Fine.           1                       []
    2   3   Okay.           1                       [hi]
    3   4   hi              3                       [How are you?]
    

    【讨论】:

    • 使用你提供的代码给了我错误:'BlockManager' object has no attribute t
    • @Luca Marinescu 嗯,你能说得更具体点吗?哪一行导致错误?什么是追溯?您能否缩小导致错误的数据的哪一部分?
    【解决方案2】:

    假设原始 Dataframe 如下所示:

             text              user_id   reply_to        
    0   this is reply to 3       1         3         
    1   this is reply to 3       2         3         
    2   this is reply to 2       3         2         
    3   this is reply to 2       4         2               
    4   this is reply to 1       5         1               
    

    然后通过使用 df.loc() 我们可以获得包含对每个文本的回复的记录:

    import pandas as pd
    
    data = [['this is reply to 3', 1, 3], ['this is reply to 3', 2, 3],['this is 
    reply to 2', 3, 2],['this is reply to 2', 4, 2], ['this is reply to 1', 5,1 ]]
    
    df = pd.DataFrame(data, columns = ['text', 'user_id', 'reply_to']) 
    
    replies = []
    
    for user_id in df.user_id:
        text = df.loc[df['reply_to'] == user_id].text.values
        replies.append(text)
    
    df['replies'] = replies
    

    生成的 Dataframe 如下所示:

             text              user_id   reply_to         replies
    0   this is reply to 3       1         3         [this is reply to 1]
    1   this is reply to 3       2         3         [this is reply to 2, this is reply to 2]
    2   this is reply to 2       3         2         [this is reply to 3, this is reply to 3]
    3   this is reply to 2       4         2               []
    4   this is reply to 1       5         1               []
    

    【讨论】:

    • 你好@AmrSherbiny!请直接将您的数据框粘贴到您的答案中,这样社区可以更轻松地阅读或复制/粘贴它。
    猜你喜欢
    • 2017-10-17
    • 2018-08-20
    • 2019-10-12
    • 2016-10-01
    • 2022-01-17
    • 2021-02-06
    • 2019-01-22
    • 2019-09-08
    • 2018-07-15
    相关资源
    最近更新 更多