简体   繁体   English

根据数据框中另一列的值创建新列

[英]Create new column based on a value of another column in a data-frame

A snippet of my current data-frame is:我当前数据框的一个片段是:

     |commentID | commentType |depth | parentID   |                                    
     |:-------- |:-------------------------------:| 
0    |58b61d1d  | comment     | 1.0  | 0.0        |
1    |58b6393b  | userReply   | 2.0  | 58b61d1d.0 |     
2    |58b6556e  | comment     | 1.0  | 0.0        |
3    |58b657fa  | userReply   | 3.0  | 58b61d1d.0 |
4    |58b657fa  | comment     | 1.0  | 0.0        |

I want the data-frame to look like:我希望数据框看起来像:

     |commentID | commentType |depth | parentID   | receiveAReply |                                  
     |:-------- |:--------------------------------|--------------:| 
0    |58b61d1d  | comment     | 1.0  | 0.0        | 1             |
1    |58b6393b  | userReply   | 2.0  | 58b61d1d.0 | 0             |
2    |58b6556e  | comment     | 1.0  | 0.0        | 0             |
3    |58b657fa  | userReply   | 3.0  | 58b61d1d.0 | 0             |
4    |58b657fa  | comment     | 1.0  | 0.0        | 0             |
  • An added column: receiveAReply添加的列:receiveAReply
  • Where if any comment receives a reply it is assigned 1. Even if a comment has multiple replies, it is still only assigned 1 or 0.如果任何评论收到回复,则分配为 1。即使评论有多个回复,它仍然只分配 1 或 0。
  • all user replies receive 0 even if that reply has a reply, eg depth = 3.0.所有用户回复都会收到 0,即使该回复有回复,例如深度 = 3.0。 Such that I only care about comments on the actual article and if they received a reply, not the number of replies or the replies to these replies.这样我只关心对实际文章的评论以及他们是否收到回复,而不是回复的数量或对这些回复的回复。
  • Therefore, I am focusing on user replies with depth 2.0 and what commentID's do their parentID's match.因此,我专注于深度 2.0 的用户回复以及他们的 parentID 匹配的commentID。

I have the following code, however it is assigning the whole receiveAReply column Nan, where I try create another column 'replies' where they it has the parent ID's of depth 2.0.我有以下代码,但是它分配了整个receiveAReply 列Nan,我尝试在其中创建另一列“回复”,其中它们具有深度为2.0 的父ID。 I tried to to assign 1 based on if any commentID's match these parent ID's:我尝试根据是否有任何commentID 与这些父ID 匹配来分配1:


df['replies'] = df.loc[df.depth == 2.0, ['parentID']]
df['receiveAReply'] = df.loc[df.commentID == df.replies, [1]]

IIUC your conditions, you just miss to extract the left part of parentID column: IIUC 您的条件,您只是错过了提取parentID列的左侧部分:

pid = df.loc[df['depth'] == 2, 'parentID'].str.split('.').str[0].values

df['receiveAReply'] = 0
df.loc[df['commentID'].isin(pid), 'receiveAReply'] = 1

Output: Output:

>>> df
  commentID commentType  depth    parentID  receiveAReply
0  58b61d1d     comment    1.0         0.0              1
1  58b6393b   userReply    2.0  58b61d1d.0              0
2  58b6556e     comment    1.0         0.0              0
3  58b657fa   userReply    3.0  58b61d1d.0              0
4  58b657fa     comment    1.0         0.0              0

This worked for me:这对我有用:

df['replies'] = df.loc[df.depth == 2.0, ['parentID']]

def test(x, y):
    if x in y.values:
        return 1
    else:
        return 0


df['getsReply'] = df['commentID'].apply(lambda x: test(x, df['replies']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM