简体   繁体   English

Pandas dataframe 使用基于上述行的值创建新列

[英]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 1 bing search engine搜索引擎 1 1 0 0 NaN
2 2 bbcnews英国广播公司的新闻 public broadcaster公共广播公司 1 1 1 1 centre中心
3 3 bbcnews英国广播公司的新闻 public broadcaster公共广播公司 1 1 1 1 centre中心
4 4 facebook facebook social media社交媒体 1 1 0 0 NaN
5 5 foxnews福克斯新闻 commercial broadcaster商业广播公司 1 1 1 1 centre中心

I want to obtain a dataframe like this:我想像这样获得 dataframe:

index指数 domain领域 type类型 upst上层 downst下降 flag旗帜 refer_fb参考fb refer_soc_med参考soc_med ref_bing ref_bing refer_search_eng refer_search_eng
1 1 bing search engine搜索引擎 1 1 0 0 NaN NaN NaN NaN NaN
2 2 bbcnews英国广播公司的新闻 public broadcaster公共广播公司 1 1 1 1 centre中心 0 0 0 0 1 1 1 1
3 3 bbcnews英国广播公司的新闻 public broadcaster公共广播公司 1 1 1 1 centre中心 0 0 0 0 1 1 1 1
4 4 facebook facebook social media社交媒体 1 1 0 0 NaN NaN NaN NaN NaN
5 5 foxnews福克斯新闻 commercial broadcaster商业广播公司 1 1 1 1 centre中心 1 1 1 1 0 0 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.

相关问题 根据具有特定条件的上述行中的一列的值创建新行 - pandas 或 numpy - create new rows based on values of one of the columns in the above row with specific condition - pandas or numpy 根据上面的行创建新的 dataframe 行 - Create new dataframe row based on row above 根据其他列行中的过滤值在 pandas dataframe 中创建一个新列 - Create a new Column in pandas dataframe based on the filetered values in the row of other columns 从熊猫数据框中的唯一行值创建新列 - Create new columns from unique row values in a pandas dataframe 根据其他列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in other columns 根据上一行的值在熊猫数据框中创建一个新列 - Create a new column in a pandas dataframe based on values found on a previous row Python pandas 根据上面某些行的条件在 dataframe 中创建一个新行 - Python pandas create a new row within dataframe based on a conditions on certain rows above 如何根据高于阈值的任何列创建 pandas dataframe - how to create pandas dataframe based on any of columns above threshold 根据条件在 Pandas DataFrame 中创建新行 - Create new row in Pandas DataFrame based on conditions 创建一个新行,它是上述行的计算结果 - Pandas DataFrame - Create a new row that is the result of a calculation of rows above - Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM