conditional-statements

Weird behaviour of `not` operator with python list

孤人 提交于 2021-02-05 05:05:29
问题 When I'm trying to check whether a list is empty or not using python's not operator it is behaving in a weird manner. I tried using the not operator with a list to check whether it is empty or not. >>> a = [] >>> not (a) True >>> not (a) == True True >>> not (a) == False True >>> True == False False The expected output for not (a) == False should be False. 回答1: == has higher precedence than not . not (a) == False is parsed as not (a == False) . 回答2: This is working as expected. Parenthesis

Weird behaviour of `not` operator with python list

混江龙づ霸主 提交于 2021-02-05 05:01:44
问题 When I'm trying to check whether a list is empty or not using python's not operator it is behaving in a weird manner. I tried using the not operator with a list to check whether it is empty or not. >>> a = [] >>> not (a) True >>> not (a) == True True >>> not (a) == False True >>> True == False False The expected output for not (a) == False should be False. 回答1: == has higher precedence than not . not (a) == False is parsed as not (a == False) . 回答2: This is working as expected. Parenthesis

Pandas: Change values chosen by boolean indexing in a column without getting a warning

走远了吗. 提交于 2021-02-05 03:21:02
问题 I have a dataframe, I want to change only those values of a column where another column fulfills a certain condition. I'm trying to do this with iloc at the moment and it either does not work or I'm getting that annoying warning: A value is trying to be set on a copy of a slice from a DataFrame Example: import pandas as pd DF = pd.DataFrame({'A':[1,1,2,1,2,2,1,2,1],'B':['a','a','b','c','x','t','i','x','b']}) Doing one of those DF['B'].iloc[:][DF['A'] == 1] = 'X' DF.iloc[:]['B'][DF['A'] == 1]

Pandas: Change values chosen by boolean indexing in a column without getting a warning

烂漫一生 提交于 2021-02-05 03:16:06
问题 I have a dataframe, I want to change only those values of a column where another column fulfills a certain condition. I'm trying to do this with iloc at the moment and it either does not work or I'm getting that annoying warning: A value is trying to be set on a copy of a slice from a DataFrame Example: import pandas as pd DF = pd.DataFrame({'A':[1,1,2,1,2,2,1,2,1],'B':['a','a','b','c','x','t','i','x','b']}) Doing one of those DF['B'].iloc[:][DF['A'] == 1] = 'X' DF.iloc[:]['B'][DF['A'] == 1]

How to group based on cumulative sum that resets on a condition

那年仲夏 提交于 2021-02-04 18:59:55
问题 I have a pandas df with word counts corresponding to articles. I want to be able to be able to add another column MERGED that is based on groups of articles that have a minimum cumulative sum of 'min_words'. df = pd.DataFrame([[ 0, 6], [ 1, 10], [ 3, 5], [ 4, 7], [ 5, 26], [ 6, 7], [ 9, 4], [ 10, 133], [ 11, 42], [ 12, 1]], columns=['ARTICLE', 'WORD_COUNT']) df Out[15]: ARTICLE WORD_COUNT 0 0 6 1 1 10 2 3 5 3 4 7 4 5 26 5 6 7 6 9 4 7 10 133 8 11 42 9 12 1 So then if min_words = 20 this is the

How to count concurrent events in a dataframe in one line?

做~自己de王妃 提交于 2021-02-04 16:09:42
问题 I have a dataset with phone calls. I want to count how many active calls there are for each record. I found this question but I'd like to avoid loops and functions. Each call has a date , a start time and a end time . The dataframe: start end date 0 09:17:12 09:18:20 2016-08-10 1 09:15:58 09:17:42 2016-08-11 2 09:16:40 09:17:49 2016-08-11 3 09:17:05 09:18:03 2016-08-11 4 09:18:22 09:18:30 2016-08-11 What I want: start end date activecalls 0 09:17:12 09:18:20 2016-08-10 1 1 09:15:58 09:17:42

How to count concurrent events in a dataframe in one line?

天大地大妈咪最大 提交于 2021-02-04 16:08:43
问题 I have a dataset with phone calls. I want to count how many active calls there are for each record. I found this question but I'd like to avoid loops and functions. Each call has a date , a start time and a end time . The dataframe: start end date 0 09:17:12 09:18:20 2016-08-10 1 09:15:58 09:17:42 2016-08-11 2 09:16:40 09:17:49 2016-08-11 3 09:17:05 09:18:03 2016-08-11 4 09:18:22 09:18:30 2016-08-11 What I want: start end date activecalls 0 09:17:12 09:18:20 2016-08-10 1 1 09:15:58 09:17:42

How to count concurrent events in a dataframe in one line?

99封情书 提交于 2021-02-04 16:08:38
问题 I have a dataset with phone calls. I want to count how many active calls there are for each record. I found this question but I'd like to avoid loops and functions. Each call has a date , a start time and a end time . The dataframe: start end date 0 09:17:12 09:18:20 2016-08-10 1 09:15:58 09:17:42 2016-08-11 2 09:16:40 09:17:49 2016-08-11 3 09:17:05 09:18:03 2016-08-11 4 09:18:22 09:18:30 2016-08-11 What I want: start end date activecalls 0 09:17:12 09:18:20 2016-08-10 1 1 09:15:58 09:17:42

How to count concurrent events in a dataframe in one line?

▼魔方 西西 提交于 2021-02-04 16:08:20
问题 I have a dataset with phone calls. I want to count how many active calls there are for each record. I found this question but I'd like to avoid loops and functions. Each call has a date , a start time and a end time . The dataframe: start end date 0 09:17:12 09:18:20 2016-08-10 1 09:15:58 09:17:42 2016-08-11 2 09:16:40 09:17:49 2016-08-11 3 09:17:05 09:18:03 2016-08-11 4 09:18:22 09:18:30 2016-08-11 What I want: start end date activecalls 0 09:17:12 09:18:20 2016-08-10 1 1 09:15:58 09:17:42

How to count concurrent events in a dataframe in one line?

天涯浪子 提交于 2021-02-04 16:08:19
问题 I have a dataset with phone calls. I want to count how many active calls there are for each record. I found this question but I'd like to avoid loops and functions. Each call has a date , a start time and a end time . The dataframe: start end date 0 09:17:12 09:18:20 2016-08-10 1 09:15:58 09:17:42 2016-08-11 2 09:16:40 09:17:49 2016-08-11 3 09:17:05 09:18:03 2016-08-11 4 09:18:22 09:18:30 2016-08-11 What I want: start end date activecalls 0 09:17:12 09:18:20 2016-08-10 1 1 09:15:58 09:17:42