in Education by
I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on the list that I have: list = [0,1,2,3,6,7,9,10] The new column is only updated with the list value if the flag (in col2) is 1. If flag is 0, do not populate row in new column. Current DF +-------------+---------+ | context | flag | +-------------+---------+ | 0 | 1 | | 0 | 1 | | 0 | 0 | | 2 | 1 | | 2 | 1 | | 2 | 1 | | 2 | 1 | | 2 | 0 | | 4 | 1 | | 4 | 1 | | 4 | 0 | +-------------+---------+ Desired DF +-------------+---------+-------------+ | context | flag | new_context | +-------------+---------+-------------+ | 0 | 1 | 0 | | 0 | 1 | 1 | | 0 | 0 | | | 2 | 1 | 2 | | 2 | 1 | 3 | | 2 | 1 | 6 | | 2 | 1 | 7 | | 2 | 0 | | | 4 | 1 | 9 | | 4 | 1 | 10 | | 4 | 0 | | +-------------+---------+-------------+ Right now, I loop through the indices of the list and assign the list value to the new_context column. Then I increment to go through the list. The values are populated in the correct spots but they all say 0. I don't believe it's iterating through the list properly. list_length = len(list) i=0 for i in range(list_length])): df["new_context"] = [list[i] if ele == 0 else "" for ele in df["flag"]] if df["flag"] == 0: i+=1 I have also tried to iterate through a entire dataframe, however I think it's just applying the same list value (first list value of 0) i=0 for index, row in df.iterrows(): df["new_context"] = [list[i] if ele == 0 else "" for ele in df["flag"]] if row['flag'] == 0: i+=1 How can I use the next list value to populate the new column where the flag=1? It seems i+=1 is not working. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Let us try l = [0,1,2,3,6,7,9,10] df['New']='' df.loc[df.flag==1,'New']=l df Out[80]: context flag New 0 0 1 0 1 0 1 1 2 0 0 3 2 1 2 4 2 1 3 5 2 1 6 6 2 1 7 7 2 0 8 4 1 9 9 4 1 10 10 4 0 Do check out Data Science with Python course which helps you understand from scratch

Related questions

0 votes
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    How do I use the for loop to execute the following operation? df_1=pd.concat([df_ab1,df_xy1], axis=1) df_2= ... Am I missing something? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date ... -01-01 FIELDZ 10.8 Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    please see my code below, example_list = [ ['a','b','c'], ['f','g','h'], ['i','j','k'], ] my_string = ''' ' ... g','h'), ('i','j','k'); Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I wrote the code for the counter in python, but I did not my appropriate result. I made the list by input numbers, ... like: [33,16,19] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to merge 2 data frames by using the id and then trying to save the result in the JSON file. | |id|a|b ... |3|15|3, 12|6, 15 Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...