idx
int64 0
2.38k
| question
stringlengths 5
184
| target
stringlengths 5
213
|
---|---|---|
400 | sort list `lst` based on each element's number of occurrences
| sorted ( lst , key = lambda x : ( - 1 * c [ x ] , lst . index ( x ) ) )
|
401 | Get the value with the maximum length in each column in array `foo`
| [ max ( len ( str ( x ) ) for x in line ) for line in zip ( * foo ) ]
|
402 | get the count of each unique value in column `Country` of dataframe `df` and store in column `Sum of Accidents`
| df . Country . value_counts ( ) . reset_index ( name = 'Sum of Accidents' )
|
403 | calculat the difference between each row and the row previous to it in dataframe `data`
| data . set_index ( 'Date' ) . diff ( )
|
404 | append values `[3, 4]` to a set `a`
| a . update ( [ 3 , 4 ] )
|
405 | set every two-stride far element to -1 starting from second element in array `a`
| a [ 1 : : 2 ] = - 1
|
406 | Get rank of rows from highest to lowest of dataframe `df`, grouped by value in column `group`, according to value in column `value`
| df . groupby ( 'group' ) [ 'value' ] . rank ( ascending = False )
|
407 | convert js date object 'Tue, 22 Nov 2011 06:00:00 GMT' to python datetime
| datetime . strptime ( 'Tue, 22 Nov 2011 06:00:00 GMT' , '%a, %d %b %Y %H:%M:%S %Z' )
|
408 | Convert a binary value '1633837924' to string
| struct . pack ( '<I' , 1633837924 )
|
409 | append string `foo` to list `list`
| list . append ( 'foo' )
|
410 | insert string `foo` at position `0` of list `list`
| list . insert ( 0 , 'foo' )
|
411 | convert keys in dictionary `thedict` into case insensitive
| theset = set ( k . lower ( ) for k in thedict )
|
412 | pad 'dog' up to a length of 5 characters with 'x'
| """{s:{c}^{n}}""" . format ( s = 'dog' , n = 5 , c = 'x' )
|
413 | check if type of variable `s` is a string
| isinstance ( s , str )
|
414 | check if type of a variable `s` is string
| isinstance ( s , str )
|
415 | Convert list of dictionaries `L` into a flat dictionary
| dict ( pair for d in L for pair in list ( d . items ( ) ) )
|
416 | merge a list of dictionaries in list `L` into a single dict
| { k : v for d in L for k , v in list ( d . items ( ) ) }
|
417 | sort a pandas data frame according to column `Peak` in ascending and `Weeks` in descending order
| df . sort_values ( [ 'Peak' , 'Weeks' ] , ascending = [ True , False ] , inplace = True )
|
418 | sort a pandas data frame by column `Peak` in ascending and `Weeks` in descending order
| df . sort ( [ 'Peak' , 'Weeks' ] , ascending = [ True , False ] , inplace = True )
|
419 | run the code contained in string "print('Hello')"
| eval ( "print('Hello')" )
|
420 | creating a list of dictionaries [{'A': 1, 'C': 4, 'B': 2, 'D': 4}, {'A': 1, 'C': 4, 'B': 1, 'D': 5}]
| [ { 'A' : 1 , 'C' : 4 , 'B' : 2 , 'D' : 4 } , { 'A' : 1 , 'C' : 4 , 'B' : 1 , 'D' : 5 } ]
|
421 | Creating a list of dictionaries in python
| [ { 'A' : 1 , 'C' : 4 , 'B' : 2 , 'D' : 4 } , { 'A' : 1 , 'C' : 4 , 'B' : 1 , 'D' : 5 } ]
|
422 | get all possible combination of items from 2-dimensional list `a`
| list ( itertools . product ( * a ) )
|
423 | Get sum of values of columns 'Y1961', 'Y1962', 'Y1963' after group by on columns "Country" and "Item_code" in dataframe `df`.
| df . groupby ( [ 'Country' , 'Item_Code' ] ) [ [ 'Y1961' , 'Y1962' , 'Y1963' ] ] . sum ( )
|
424 | create list `done` containing permutations of each element in list `[a, b, c, d]` with variable `x` as tuples
| done = [ ( el , x ) for el in [ a , b , c , d ] ]
|
425 | remove Nan values from array `x`
| x = x [ numpy . logical_not ( numpy . isnan ( x ) ) ]
|
426 | remove first directory from path '/First/Second/Third/Fourth/Fifth'
| os . path . join ( * x . split ( os . path . sep ) [ 2 : ] )
|
427 | Replace `;` with `:` in a string `line`
| line = line . replace ( ';' , ':' )
|
428 | call bash command 'tar c my_dir | md5sum' with pipe
| subprocess . call ( 'tar c my_dir | md5sum' , shell = True )
|
429 | Convert a hex string `437c2123 ` according to ascii value.
| """437c2123""" . decode ( 'hex' )
|
430 | Get a list of all fields in class `User` that are marked `required`
| [ k for k , v in User . _fields . items ( ) if v . required ]
|
431 | remove column by index `[:, 0:2]` in dataframe `df`
| df = df . ix [ : , 0 : 2 ]
|
432 | change a string of integers `x` separated by spaces to a list of int
| x = map ( int , x . split ( ) )
|
433 | convert a string of integers `x` separated by spaces to a list of integers
| x = [ int ( i ) for i in x . split ( ) ]
|
434 | find element by css selector "input[onclick*='1 Bedroom Deluxe']"
| driver . find_element_by_css_selector ( "input[onclick*='1 Bedroom Deluxe']" )
|
435 | Python / Remove special character from string
| re . sub ( '[^a-zA-Z0-9-_*.]' , '' , my_string )
|
436 | display a pdf file that has been downloaded as `my_pdf.pdf`
| webbrowser . open ( 'file:///my_pdf.pdf' )
|
437 | replace backslashes in string `result` with empty string ''
| result = result . replace ( '\\' , '' )
|
438 | remove backslashes from string `result`
| result . replace ( '\\' , '' )
|
439 | replace value '-' in any column of pandas dataframe to "NaN"
| df . replace ( '-' , 'NaN' )
|
440 | convert datetime object to date object in python
| datetime . datetime . now ( ) . date ( )
|
441 | How do I convert datetime to date (in Python)?
| datetime . datetime . now ( ) . date ( )
|
442 | get all sub-elements of an element `a` in an elementtree
| [ elem . tag for elem in a . iter ( ) ]
|
443 | get all sub-elements of an element tree `a` excluding the root element
| [ elem . tag for elem in a . iter ( ) if elem is not a ]
|
444 | How can I split and parse a string in Python?
| """2.7.0_bf4fda703454""" . split ( '_' )
|
445 | move dictionaries in list `lst` to the end of the list if value of key 'language' in each dictionary is not equal to 'en'
| sorted ( lst , key = lambda x : x [ 'language' ] != 'en' )
|
446 | check if all values of a dictionary `your_dict` are zero `0`
| all ( value == 0 for value in list ( your_dict . values ( ) ) )
|
447 | produce a pivot table as dataframe using column 'Y' in datafram `df` to form the axes of the resulting dataframe
| df . pivot_table ( 'Y' , rows = 'X' , cols = 'X2' )
|
448 | call `doSomething()` in a try-except without handling the exception
| try : doSomething ( ) except : pass
|
449 | call `doSomething()` in a try-except without handling the exception
| try : doSomething ( ) except Exception : pass
|
450 | get a sum of 4d array `M`
| M . sum ( axis = 0 ) . sum ( axis = 0 )
|
451 | Convert a datetime object `dt` to microtime
| time . mktime ( dt . timetuple ( ) ) + dt . microsecond / 1000000.0
|
452 | select all rows in dataframe `df` where the values of column 'columnX' is bigger than or equal to `x` and smaller than or equal to `y`
| df [ ( x <= df [ 'columnX' ] ) & ( df [ 'columnX' ] <= y ) ]
|
453 | sort a list of lists `L` by index 2 of the inner list
| sorted ( L , key = itemgetter ( 2 ) )
|
454 | sort a list of lists `l` by index 2 of the inner list
| l . sort ( key = ( lambda x : x [ 2 ] ) )
|
455 | sort list `l` by index 2 of the item
| sorted ( l , key = ( lambda x : x [ 2 ] ) )
|
456 | sort a list of lists `list_to_sort` by indices 2,0,1 of the inner list
| sorted_list = sorted ( list_to_sort , key = itemgetter ( 2 , 0 , 1 ) )
|
457 | find rows of 2d array in 3d numpy array 'arr' if the row has value '[[0, 3], [3, 0]]'
| np . argwhere ( np . all ( arr == [ [ 0 , 3 ] , [ 3 , 0 ] ] , axis = ( 1 , 2 ) ) )
|
458 | From multiIndexed dataframe `data` select columns `a` and `c` within each higher order column `one` and `two`
| data . loc [ : , ( list ( itertools . product ( [ 'one' , 'two' ] , [ 'a' , 'c' ] ) ) ) ]
|
459 | select only specific columns 'a' and 'c' from a dataframe 'data' with multiindex columns
| data . loc [ : , ( [ ( 'one' , 'a' ) , ( 'one' , 'c' ) , ( 'two' , 'a' ) , ( 'two' , 'c' ) ] ) ]
|
460 | match a sharp, followed by letters (including accent characters) in string `str1` using a regex
| hashtags = re . findall ( '#(\\w+)' , str1 , re . UNICODE )
|
461 | Rename file from `src` to `dst`
| os . rename ( src , dst )
|
462 | Get all texts and tags from a tag `strong` from etree tag `some_tag` using lxml
| print ( etree . tostring ( some_tag . find ( 'strong' ) ) )
|
463 | Serialize dictionary `data` and its keys to a JSON formatted string
| json . dumps ( { str ( k ) : v for k , v in data . items ( ) } )
|
464 | parse UTF-8 encoded HTML response `response` to BeautifulSoup object
| soup = BeautifulSoup ( response . read ( ) . decode ( 'utf-8' ) )
|
465 | delete file `filename`
| os . remove ( filename )
|
466 | get the next value greatest to `2` from a list of numbers `num_list`
| min ( [ x for x in num_list if x > 2 ] )
|
467 | Replace each value in column 'prod_type' of dataframe `df` with string 'responsive'
| df [ 'prod_type' ] = 'responsive'
|
468 | sort list `lst` with positives coming before negatives with values sorted respectively
| sorted ( lst , key = lambda x : ( x < 0 , x ) )
|
469 | get the date 6 months from today
| six_months = ( date . today ( ) + relativedelta ( months = ( + 6 ) ) )
|
470 | get the date 1 month from today
| ( date ( 2010 , 12 , 31 ) + relativedelta ( months = ( + 1 ) ) )
|
471 | get the date 2 months from today
| ( date ( 2010 , 12 , 31 ) + relativedelta ( months = ( + 2 ) ) )
|
472 | calculate the date six months from the current date
| print ( ( datetime . date . today ( ) + datetime . timedelta ( ( ( 6 * 365 ) / 12 ) ) ) . isoformat ( ) )
|
473 | get a list of keys of dictionary `things` sorted by the value of nested dictionary key 'weight'
| sorted ( list ( things . keys ( ) ) , key = lambda x : things [ x ] [ 'weight' ] , reverse = True )
|
474 | get all the values from a numpy array `a` excluding index 3
| a [ np . arange ( len ( a ) ) != 3 ]
|
475 | delete all elements from a list `x` if a function `fn` taking value as parameter returns `0`
| [ x for x in lst if fn ( x ) != 0 ]
|
476 | set dataframe `df` index using column 'month'
| df . set_index ( 'month' )
|
477 | read lines from a csv file `./urls-eu.csv` into a list of lists `arr`
| arr = [ line . split ( ',' ) for line in open ( './urls-eu.csv' ) ]
|
478 | list comprehension that produces integers between 11 and 19
| [ i for i in range ( 100 ) if i > 10 if i < 20 ]
|
479 | Get only digits from a string `strs`
| """""" . join ( [ c for c in strs if c . isdigit ( ) ] )
|
480 | split a string `yas` based on tab '\t'
| re . split ( '\\t+' , yas . rstrip ( '\t' ) )
|
481 | scalar multiply matrix `a` by `b`
| ( a . T * b ) . T
|
482 | remove trailing newline in string "test string\n"
| 'test string\n' . rstrip ( )
|
483 | remove trailing newline in string 'test string \n\n'
| 'test string \n\n' . rstrip ( '\n' )
|
484 | remove newline in string `s`
| s . strip ( )
|
485 | remove newline in string `s` on the right side
| s . rstrip ( )
|
486 | remove newline in string `s` on the left side
| s . lstrip ( )
|
487 | remove newline in string 'Mac EOL\r'
| 'Mac EOL\r' . rstrip ( '\r\n' )
|
488 | remove newline in string 'Windows EOL\r\n' on the right side
| 'Windows EOL\r\n' . rstrip ( '\r\n' )
|
489 | remove newline in string 'Unix EOL\n' on the right side
| 'Unix EOL\n' . rstrip ( '\r\n' )
|
490 | remove newline in string "Hello\n\n\n" on the right side
| 'Hello\n\n\n' . rstrip ( '\n' )
|
491 | split string `text` into chunks of 16 characters each
| re . findall ( '.{,16}\\b' , text )
|
492 | Get a list comprehension in list of lists `X`
| [ [ X [ i ] [ j ] for j in range ( len ( X [ i ] ) ) ] for i in range ( len ( X ) ) ]
|
493 | convert unicode string '\xd0\xbc\xd0\xb0\xd1\x80\xd0\xba\xd0\xb0' to byte string
| '\xd0\xbc\xd0\xb0\xd1\x80\xd0\xba\xd0\xb0' . encode ( 'latin-1' )
|
494 | split dataframe `df` where the value of column `a` is equal to 'B'
| df . groupby ( ( df . a == 'B' ) . shift ( 1 ) . fillna ( 0 ) . cumsum ( ) )
|
495 | save json output from a url ‘http://search.twitter.com/search.json?q=hi’ to file ‘hi.json’ in Python 2
| urllib . request . urlretrieve ( 'http://search.twitter.com/search.json?q=hi' , 'hi.json' )
|
496 | Find indices of elements equal to zero from numpy array `x`
| numpy . where ( ( x == 0 ) ) [ 0 ]
|
497 | flush output of python print
| sys . stdout . flush ( )
|
498 | convert `i` to string
| str ( i )
|
499 | convert `a` to string
| a . __str__ ( )
|