idx
int64 0
2.38k
| question
stringlengths 5
184
| target
stringlengths 5
213
|
---|---|---|
2,100 | custom sort an alphanumeric list `l`
| sorted ( l , key = lambda x : x . replace ( '0' , 'Z' ) )
|
2,101 | plot logarithmic axes with matplotlib
| ax . set_yscale ( 'log' )
|
2,102 | Access environment variable "HOME"
| os . environ [ 'HOME' ]
|
2,103 | get value of environment variable "HOME"
| os . environ [ 'HOME' ]
|
2,104 | print all environment variables
| print ( os . environ )
|
2,105 | get all environment variables
| os . environ
|
2,106 | get value of the environment variable 'KEY_THAT_MIGHT_EXIST'
| print ( os . environ . get ( 'KEY_THAT_MIGHT_EXIST' ) )
|
2,107 | get value of the environment variable 'KEY_THAT_MIGHT_EXIST' with default value `default_value`
| print ( os . getenv ( 'KEY_THAT_MIGHT_EXIST' , default_value ) )
|
2,108 | get value of the environment variable 'HOME' with default value '/home/username/'
| print ( os . environ . get ( 'HOME' , '/home/username/' ) )
|
2,109 | create a dictionary containing each string in list `my_list` split by '=' as a key/value pairs
| print ( dict ( [ s . split ( '=' ) for s in my_list ] ) )
|
2,110 | find the index of element closest to number 11.5 in list `a`
| min ( enumerate ( a ) , key = lambda x : abs ( x [ 1 ] - 11.5 ) )
|
2,111 | find element `a` that contains string "TEXT A" in file `root`
| e = root . xpath ( './/a[contains(text(),"TEXT A")]' )
|
2,112 | Find the`a` tag in html `root` which starts with the text `TEXT A` and assign it to `e`
| e = root . xpath ( './/a[starts-with(text(),"TEXT A")]' )
|
2,113 | find the element that holds string 'TEXT A' in file `root`
| e = root . xpath ( './/a[text()="TEXT A"]' )
|
2,114 | create list `c` containing items from list `b` whose index is in list `index`
| c = [ b [ i ] for i in index ]
|
2,115 | get the dot product of two one dimensional numpy arrays
| np . dot ( a [ : , ( None ) ] , b [ ( None ) , : ] )
|
2,116 | multiplication of two 1-dimensional arrays in numpy
| np . outer ( a , b )
|
2,117 | execute a file './abc.py' with arguments `arg1` and `arg2` in python shell
| subprocess . call ( [ './abc.py' , arg1 , arg2 ] )
|
2,118 | Replace NaN values in column 'value' with the mean of data in column 'group' of dataframe `df`
| df [ [ 'value' ] ] . fillna ( df . groupby ( 'group' ) . transform ( 'mean' ) )
|
2,119 | separate each character in string `s` by '-'
| re . sub ( '(.)(?=.)' , '\\1-' , s )
|
2,120 | concatenate '-' in between characters of string `str`
| re . sub ( '(?<=.)(?=.)' , '-' , str )
|
2,121 | get the indexes of the x and y axes in Numpy array `np` where variable `a` is equal to variable `value`
| i , j = np . where ( a == value )
|
2,122 | print letter that appears most frequently in string `s`
| print ( collections . Counter ( s ) . most_common ( 1 ) [ 0 ] )
|
2,123 | find float number proceeding sub-string `par` in string `dir`
| float ( re . findall ( '(?:^|_)' + par + '(\\d+\\.\\d*)' , dir ) [ 0 ] )
|
2,124 | Get all the matches from a string `abcd` if it begins with a character `a`
| re . findall ( '[^a]' , 'abcd' )
|
2,125 | get a list of variables from module 'adfix.py' in current module.
| print ( [ item for item in dir ( adfix ) if not item . startswith ( '__' ) ] )
|
2,126 | get the first element of each tuple in a list `rows`
| [ x [ 0 ] for x in rows ]
|
2,127 | get a list `res_list` of the first elements of each tuple in a list of tuples `rows`
| res_list = [ x [ 0 ] for x in rows ]
|
2,128 | duplicate data in pandas dataframe `x` for 5 times
| pd . concat ( [ x ] * 5 , ignore_index = True )
|
2,129 | Get a repeated pandas data frame object `x` by `5` times
| pd . concat ( [ x ] * 5 )
|
2,130 | sort json `ips_data` by a key 'data_two'
| sorted_list_of_keyvalues = sorted ( list ( ips_data . items ( ) ) , key = item [ 1 ] [ 'data_two' ] )
|
2,131 | read json `elevations` to pandas dataframe `df`
| pd . read_json ( elevations )
|
2,132 | generate a random number in 1 to 7 with a given distribution [0.1, 0.05, 0.05, 0.2, 0.4, 0.2]
| numpy . random . choice ( numpy . arange ( 1 , 7 ) , p = [ 0.1 , 0.05 , 0.05 , 0.2 , 0.4 , 0.2 ] )
|
2,133 | Return rows of data associated with the maximum value of column 'Value' in dataframe `df`
| df . loc [ df [ 'Value' ] . idxmax ( ) ]
|
2,134 | find recurring patterns in a string '42344343434'
| re . findall ( '^(.+?)((.+)\\3+)$' , '42344343434' ) [ 0 ] [ : - 1 ]
|
2,135 | convert binary string '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@' to numpy array
| np . fromstring ( '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@' , dtype = '<f4' )
|
2,136 | convert binary string to numpy array
| np . fromstring ( '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@' , dtype = '>f4' )
|
2,137 | insert variables `(var1, var2, var3)` into sql statement 'INSERT INTO table VALUES (?, ?, ?)'
| cursor . execute ( 'INSERT INTO table VALUES (?, ?, ?)' , ( var1 , var2 , var3 ) )
|
2,138 | Execute a sql statement using variables `var1`, `var2` and `var3`
| cursor . execute ( 'INSERT INTO table VALUES (%s, %s, %s)' , ( var1 , var2 , var3 ) )
|
2,139 | How to use variables in SQL statement in Python?
| cursor . execute ( 'INSERT INTO table VALUES (%s, %s, %s)' , ( var1 , var2 , var3 ) )
|
2,140 | pandas split strings in column 'stats' by ',' into columns in dataframe `df`
| df [ 'stats' ] . str [ 1 : - 1 ] . str . split ( ',' , expand = True ) . astype ( float )
|
2,141 | split string in column 'stats' by ',' into separate columns in dataframe `df`
| df [ 'stats' ] . str [ 1 : - 1 ] . str . split ( ',' ) . apply ( pd . Series ) . astype ( float )
|
2,142 | Unpack column 'stats' in dataframe `df` into a series of columns
| df [ 'stats' ] . apply ( pd . Series )
|
2,143 | wait for shell command `p` evoked by subprocess.Popen to complete
| p . wait ( )
|
2,144 | encode string `s` to utf-8 code
| s . encode ( 'utf8' )
|
2,145 | parse string '01-Jan-1995' into a datetime object using format '%d-%b-%Y'
| datetime . datetime . strptime ( '01-Jan-1995' , '%d-%b-%Y' )
|
2,146 | copy a file from `src` to `dst`
| copyfile ( src , dst )
|
2,147 | copy file "/dir/file.ext" to "/new/dir/newname.ext"
| shutil . copy2 ( '/dir/file.ext' , '/new/dir/newname.ext' )
|
2,148 | copy file '/dir/file.ext' to '/new/dir'
| shutil . copy2 ( '/dir/file.ext' , '/new/dir' )
|
2,149 | print a list of integers `list_of_ints` using string formatting
| print ( ', ' . join ( str ( x ) for x in list_of_ints ) )
|
2,150 | multiply column 'A' and column 'B' by column 'C' in datafram `df`
| df [ [ 'A' , 'B' ] ] . multiply ( df [ 'C' ] , axis = 'index' )
|
2,151 | convert string 'a' to hex
| hex ( ord ( 'a' ) )
|
2,152 | Get the sum of values to the power of their indices in a list `l`
| sum ( j ** i for i , j in enumerate ( l , 1 ) )
|
2,153 | remove extra white spaces & tabs from a string `s`
| """ """ . join ( s . split ( ) )
|
2,154 | replace comma in string `s` with empty string ''
| s = s . replace ( ',' , '' )
|
2,155 | Resample dataframe `frame` to resolution of 1 hour `1H` for timeseries index, summing values in the column `radiation` averaging those in column `tamb`
| frame . resample ( '1H' ) . agg ( { 'radiation' : np . sum , 'tamb' : np . mean } )
|
2,156 | How do I get rid of Python Tkinter root window?
| root . destroy ( )
|
2,157 | create a pandas dataframe `df` from elements of a dictionary `nvalues`
| df = pd . DataFrame . from_dict ( { k : v for k , v in list ( nvalues . items ( ) ) if k != 'y3' } )
|
2,158 | Flask get value of request variable 'firstname'
| first_name = request . args . get ( 'firstname' )
|
2,159 | Flask get posted form data 'firstname'
| first_name = request . form . get ( 'firstname' )
|
2,160 | get a list of substrings consisting of the first 5 characters of every string in list `buckets`
| [ s [ : 5 ] for s in buckets ]
|
2,161 | sort list `the_list` by the length of string followed by alphabetical order
| the_list . sort ( key = lambda item : ( - len ( item ) , item ) )
|
2,162 | Set index equal to field 'TRX_DATE' in dataframe `df`
| df = df . set_index ( [ 'TRX_DATE' ] )
|
2,163 | List comprehension with an accumulator in range of 10
| list ( accumulate ( list ( range ( 10 ) ) ) )
|
2,164 | How to convert a date string '2013-1-25' in format '%Y-%m-%d' to different format '%m/%d/%y'
| datetime . datetime . strptime ( '2013-1-25' , '%Y-%m-%d' ) . strftime ( '%m/%d/%y' )
|
2,165 | convert a date string '2013-1-25' in format '%Y-%m-%d' to different format '%-m/%d/%y'
| datetime . datetime . strptime ( '2013-1-25' , '%Y-%m-%d' ) . strftime ( '%-m/%d/%y' )
|
2,166 | get a dataframe `df2` that contains all the columns of dataframe `df` that do not end in `prefix`
| df2 = df . ix [ : , ( ~ df . columns . str . endswith ( 'prefix' ) ) ]
|
2,167 | create list `new_list` containing the last 10 elements of list `my_list`
| new_list = my_list [ - 10 : ]
|
2,168 | get the last 10 elements from a list `my_list`
| my_list [ - 10 : ]
|
2,169 | convert matlab engine array `x` to a numpy ndarray
| np . array ( x . _data ) . reshape ( x . size [ : : - 1 ] ) . T
|
2,170 | select the first row grouped per level 0 of dataframe `df`
| df . groupby ( level = 0 , as_index = False ) . nth ( 0 )
|
2,171 | concatenate sequence of numpy arrays `LIST` into a one dimensional array along the first axis
| numpy . concatenate ( LIST , axis = 0 )
|
2,172 | convert and escape string "\\xc3\\x85γ" to UTF-8 code
| """\\xc3\\x85γ""" . encode ( 'utf-8' ) . decode ( 'unicode_escape' )
|
2,173 | encode string "\\xc3\\x85γ" to bytes
| """\\xc3\\x85γ""" . encode ( 'utf-8' )
|
2,174 | interleave the elements of two lists `a` and `b`
| [ j for i in zip ( a , b ) for j in i ]
|
2,175 | merge two lists `a` and `b` into a single list
| [ j for i in zip ( a , b ) for j in i ]
|
2,176 | delete all occureces of `8` in each string `s` in list `lst`
| print ( [ s . replace ( '8' , '' ) for s in lst ] )
|
2,177 | Split string `Hello` into a string of letters seperated by `,`
| """,""" . join ( 'Hello' )
|
2,178 | in Django, select 100 random records from the database `Content.objects`
| Content . objects . all ( ) . order_by ( '?' ) [ : 100 ]
|
2,179 | create a NumPy array containing elements of array `A` as pointed to by index in array `B`
| A [ np . arange ( A . shape [ 0 ] ) [ : , ( None ) ] , B ]
|
2,180 | pivot dataframe `df` so that values for `upc` become column headings and values for `saleid` become the index
| df . pivot_table ( index = 'saleid' , columns = 'upc' , aggfunc = 'size' , fill_value = 0 )
|
2,181 | match zero-or-more instances of lower case alphabet characters in a string `f233op `
| re . findall ( '([a-z]*)' , 'f233op' )
|
2,182 | match zero-or-more instances of lower case alphabet characters in a string `f233op `
| re . findall ( '([a-z])*' , 'f233op' )
|
2,183 | split string 'happy_hats_for_cats' using string '_for_'
| re . split ( '_for_' , 'happy_hats_for_cats' )
|
2,184 | Split string 'sad_pandas_and_happy_cats_for_people' based on string 'and', 'or' or 'for'
| re . split ( '_(?:for|or|and)_' , 'sad_pandas_and_happy_cats_for_people' )
|
2,185 | Split a string `l` by multiple words `for` or `or` or `and`
| [ re . split ( '_(?:f?or|and)_' , s ) for s in l ]
|
2,186 | zip keys with individual values in lists `k` and `v`
| [ dict ( zip ( k , x ) ) for x in v ]
|
2,187 | Sort a list 'lst' in descending order.
| sorted ( lst , reverse = True )
|
2,188 | sort array `order_array` based on column 'year', 'month' and 'day'
| order_array . sort ( order = [ 'year' , 'month' , 'day' ] )
|
2,189 | Sort a structured numpy array 'df' on multiple columns 'year', 'month' and 'day'.
| df . sort ( [ 'year' , 'month' , 'day' ] )
|
2,190 | check if elements in list `my_list` are coherent in order
| return my_list == list ( range ( my_list [ 0 ] , my_list [ - 1 ] + 1 ) )
|
2,191 | group rows of pandas dataframe `df` with same 'id'
| df . groupby ( 'id' ) . agg ( lambda x : x . tolist ( ) )
|
2,192 | encode `u'X\xc3\xbcY\xc3\x9f'` as unicode and decode with utf-8
| 'X\xc3\xbcY\xc3\x9f' . encode ( 'raw_unicode_escape' ) . decode ( 'utf-8' )
|
2,193 | parse string `a` to float
| float ( a )
|
2,194 | Parse String `s` to Float or Int
| try : return int ( s ) except ValueError : return float ( s )
|
2,195 | check if object `a` has property 'property'
| if hasattr ( a , 'property' ) : pass
|
2,196 | check if object `a` has property 'property'
| if hasattr ( a , 'property' ) : pass
|
2,197 | get the value of attribute 'property' of object `a` with default value 'default value'
| getattr ( a , 'property' , 'default value' )
|
2,198 | delete every 8th column in a numpy array 'a'.
| np . delete ( a , list ( range ( 0 , a . shape [ 1 ] , 8 ) ) , axis = 1 )
|
2,199 | convert `ms` milliseconds to a datetime object
| datetime . datetime . fromtimestamp ( ms / 1000.0 )
|
Subsets and Splits