Currently, I have a dataframe like this:
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 | social media | 1 | 0 | NaN | |
5 | foxnews | commercial broadcaster | 1 | 1 | centre |
I want to obtain a dataframe like this:
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 | 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). 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.