'How to change the 'True' boolean to 'False' boolean incase we have only one 'True' boolean between two 'False' boalean in a column in dataframe
I have unknown number of dataframes. The number and location of 'True' boolean are unknown in a column "label_number_hours" in dataframes. There is a possibility to be unlimited numbers of 'True' booleans between two 'False' booleans in a column "label_number_hours" in dataframes. I am looking to change the 'True' boolean to 'False' boolean in this column if the number of 'True' boolean is only one, for example, False - True - False, I want to be False - False - False.
This is an example of one of dataframe I have:
df =
label_number_hours some_other_column
0 True 0.174998
1 False 0.235088
2 True 0.076127
3 True 0.817929
4 True 0.781144
5 False 0.904597
6 True 0.703006
7 False 0.923654
8 True 0.261100
9 True 0.803631
10 False 0.149026
This is the dataframe which I am looking for:
df =
label_number_hours some_other_column
0 True 0.174998
1 False 0.235088
2 True 0.076127
3 True 0.817929
4 True 0.781144
5 False 0.904597
6 False 0.703006
7 False 0.923654
8 True 0.261100
9 True 0.803631
10 False 0.149026
This is the code:
falses_idx, = np.where(~df["label_number_hours"]) if falses_idx.size > 0: df.iloc[falses_idx[0]:falses_idx[-1], df.columns.get_loc("label_number_hours")] = False
This is the result:
label_number_hours some_other_column
0 True 0.174998
1 False 0.235088
2 False 0.076127
3 False 0.817929
4 False 0.781144
5 False 0.904597
6 False 0.703006
7 False 0.923654
8 False 0.261100
9 False 0.803631
10 False 0.149026
I need really to your help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
