[英]Pandas dataframe create new columns with values based on above row
Currently, I have a dataframe like this:目前,我有一个像这样的 dataframe :
index![]() |
domain![]() |
type![]() |
upstream![]() |
downstream![]() |
flag![]() |
---|---|---|---|---|---|
1 ![]() |
bing![]() |
search engine![]() |
1 ![]() |
0 ![]() |
NaN![]() |
2 ![]() |
bbcnews![]() |
public broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
3 ![]() |
bbcnews![]() |
public broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
4 ![]() |
facebook ![]() |
social media![]() |
1 ![]() |
0 ![]() |
NaN![]() |
5 ![]() |
foxnews![]() |
commercial broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
I want to obtain a dataframe like this:我想像这样获得 dataframe:
index![]() |
domain![]() |
type![]() |
upst![]() |
downst![]() |
flag![]() |
refer_fb![]() |
refer_soc_med![]() |
ref_bing ![]() |
refer_search_eng ![]() |
---|---|---|---|---|---|---|---|---|---|
1 ![]() |
bing![]() |
search engine![]() |
1 ![]() |
0 ![]() |
NaN![]() |
NaN![]() |
NaN![]() |
NaN![]() |
NaN![]() |
2 ![]() |
bbcnews![]() |
public broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
0 ![]() |
0 ![]() |
1 ![]() |
1 ![]() |
3 ![]() |
bbcnews![]() |
public broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
0 ![]() |
0 ![]() |
1 ![]() |
1 ![]() |
4 ![]() |
facebook ![]() |
social media![]() |
1 ![]() |
0 ![]() |
NaN![]() |
NaN![]() |
NaN![]() |
NaN![]() |
NaN![]() |
5 ![]() |
foxnews![]() |
commercial broadcaster![]() |
1 ![]() |
1 ![]() |
centre![]() |
1 ![]() |
1 ![]() |
0 ![]() |
0 ![]() |
What my script needs to do is:我的脚本需要做的是:
Create new columns, which classify each news item (always flagged as centre) according to the previous row when the previous row satisfies the condition of upstream = 1, downstream = 0. There are 6 categories of news (eg, comm broadcaster, public broadcaster are just examples).创建新列,当上一行满足upstream = 1,downstream = 0的条件时,根据上一行对每个新闻项(始终标记为中心)进行分类。新闻有6类(例如,comm broadcaster,public broadcaster)只是例子)。 I want binary values in the new columns, such as in the above example.
我想要新列中的二进制值,例如上面的示例。
Importantly, if the subsequent row after a 'news' type is also 'news' shown by 'centre' flag, then this should also be classified the same as what the previous news row was classified.重要的是,如果“新闻”类型之后的后续行也是“中心”标志显示的“新闻”,那么这也应该与前一个新闻行的分类相同。
What I understood from your question is that you want to create new columns based on values of previous columns.我从您的问题中了解到的是,您想根据先前列的值创建新列。
df["new_column_nam"] = df[(df[upst] == 1) & (df[downst] == 0)]
In place of new_column_name, you could use the column names that you want to create.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.