conditional-statements

“IndexError: positional indexers are out-of-bounds” when they're demonstrably not

喜欢而已 提交于 2020-12-29 05:36:46
问题 Here's an MWE of some code I'm using. I slowly whittle down an initial dataframe via slicing and some conditions until I have only the rows that I need. Each block of five rows actually represents a different object so that, as I whittle things down, if any one row in each block of five meets the criteria, I want to keep it -- this is what the loop over keep.index accomplishes. No matter what, when I'm done I can see that the final indices I want exist, but I get an error message saying

“IndexError: positional indexers are out-of-bounds” when they're demonstrably not

醉酒当歌 提交于 2020-12-29 05:35:45
问题 Here's an MWE of some code I'm using. I slowly whittle down an initial dataframe via slicing and some conditions until I have only the rows that I need. Each block of five rows actually represents a different object so that, as I whittle things down, if any one row in each block of five meets the criteria, I want to keep it -- this is what the loop over keep.index accomplishes. No matter what, when I'm done I can see that the final indices I want exist, but I get an error message saying

“IndexError: positional indexers are out-of-bounds” when they're demonstrably not

旧街凉风 提交于 2020-12-29 05:34:46
问题 Here's an MWE of some code I'm using. I slowly whittle down an initial dataframe via slicing and some conditions until I have only the rows that I need. Each block of five rows actually represents a different object so that, as I whittle things down, if any one row in each block of five meets the criteria, I want to keep it -- this is what the loop over keep.index accomplishes. No matter what, when I'm done I can see that the final indices I want exist, but I get an error message saying

“IndexError: positional indexers are out-of-bounds” when they're demonstrably not

放肆的年华 提交于 2020-12-29 05:34:25
问题 Here's an MWE of some code I'm using. I slowly whittle down an initial dataframe via slicing and some conditions until I have only the rows that I need. Each block of five rows actually represents a different object so that, as I whittle things down, if any one row in each block of five meets the criteria, I want to keep it -- this is what the loop over keep.index accomplishes. No matter what, when I'm done I can see that the final indices I want exist, but I get an error message saying

Python: Sum values in DataFrame if other values match between DataFrames

回眸只為那壹抹淺笑 提交于 2020-12-26 03:58:57
问题 I have two dataframes of different length like those: DataFrame A: FirstName LastName Adam Smith John Johnson DataFrame B: First Last Value Adam Smith 1.2 Adam Smith 1.5 Adam Smith 3.0 John Johnson 2.5 Imagine that what I want to do is to create a new column in "DataFrame A" summing all the values with matching last names, so the output in "A" would be: FirstName LastName Sums Adam Smith 5.7 John Johnson 2.5 If I were in Excel, I'd use =SUMIF(dfB!B:B, B2, dfB!C:C) In Python I've been trying

Fill rows in column A with value of column B if condition in column A is met

佐手、 提交于 2020-12-13 21:03:32
问题 I have a table like: colA | colB " " | 1 "K 111" | 1 "K222" | 2 " " | 3 Some columns have only a space (" "), some have "K {number}", some have "K{number}". If colA has a space I want that value replaced with the one from colB. So endresult should be: colA | colB 1 | 1 "K abc" | 1 "Kdef" | 2 3 | 3 How can I do this? 回答1: You can use a case expression: select (case when colA = ' ' then to_char(col_b) else colA end) as new_colA If you wanted to be more general, you might use like : select (case

Fill rows in column A with value of column B if condition in column A is met

蹲街弑〆低调 提交于 2020-12-13 21:00:43
问题 I have a table like: colA | colB " " | 1 "K 111" | 1 "K222" | 2 " " | 3 Some columns have only a space (" "), some have "K {number}", some have "K{number}". If colA has a space I want that value replaced with the one from colB. So endresult should be: colA | colB 1 | 1 "K abc" | 1 "Kdef" | 2 3 | 3 How can I do this? 回答1: You can use a case expression: select (case when colA = ' ' then to_char(col_b) else colA end) as new_colA If you wanted to be more general, you might use like : select (case

Pandas conditional comparison: based on multiple columns

廉价感情. 提交于 2020-12-13 03:37:54
问题 I have a df col1 col2 col3 col4 0 1 2 3 4 1 2 2 3 4 2 3 4 3 5 3 4 3 2 1 And I want to add a new column based on: if (col1 & col2) < (col3 & col4) --- > 2 I followed the approach similar to this post, just without max() as follow but all didn't work: df[['col1','col2']] < df[['col3','col4']] (df['col1'] and df['col2']) < (df['col3'] and df['col4']) What's the correct way to do it? Thanks. 回答1: mask = df[['col1','col2']].max(1) < df[['col3','col4']].min(1) df['new_col'] = np.where(mask, 2, np

Pandas conditional comparison: based on multiple columns

て烟熏妆下的殇ゞ 提交于 2020-12-13 03:31:40
问题 I have a df col1 col2 col3 col4 0 1 2 3 4 1 2 2 3 4 2 3 4 3 5 3 4 3 2 1 And I want to add a new column based on: if (col1 & col2) < (col3 & col4) --- > 2 I followed the approach similar to this post, just without max() as follow but all didn't work: df[['col1','col2']] < df[['col3','col4']] (df['col1'] and df['col2']) < (df['col3'] and df['col4']) What's the correct way to do it? Thanks. 回答1: mask = df[['col1','col2']].max(1) < df[['col3','col4']].min(1) df['new_col'] = np.where(mask, 2, np

How to use stream on method that return boolean value with condition

谁说胖子不能爱 提交于 2020-12-08 06:46:40
问题 I am using this method: public boolean checkRowsFilterNameBy(String filter){ waitForElmContainsText(searchButton, "Search"); List<AuditRow> listRows = auditTable.getTable(); for(AuditRow row : listRows){ if(!row.nameStr.equals(filter)||!row.nameStr.contains(filter)) return false; } return true; } and I want to be able to change it using Stream , I've tried the following, but I am missing something: listRows.stream().forEach(auditRow -> { if(auditRow.actionStr.equals(filter))return true;} else