목록전체 글 (462)
Note
How to get the row number of the nth largest value in a column? # Input df = pd.DataFrame(np.random.randint(1, 30, 30).reshape(10,-1), columns=list('abc')) # Solution n = 5 df['a'].argsort()[::-1][n] # output a b c 0 27 7 25 1 8 4 20 2 1 7 17 3 24 9 17 4 21 15 9 5 21 16 20 6 19 27 25 7 12 8 20 8 11 16 28 9 24 13 4 4
How to convert an array of arrays into a flat 1d array? # Input: arr1 = np.arange(3) arr2 = np.arange(3,7) arr3 = np.arange(7,10) array_of_arrays = np.array([arr1, arr2, arr3]) print('array_of_arrays: ', array_of_arrays) # Solution 1 arr_2d = np.array([a for arr in array_of_arrays for a in arr]) # Solution 2: arr_2d = np.concatenate(array_of_arrays) print(arr_2d) # output [0 1 2 3 4 5 6 7 8 9]
How to create a primary key index by combining relevant columns? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv', usecols=[0,1,2,3,5]) # Solution df[['Manufacturer', 'Model', 'Type']] = df[['Manufacturer', 'Model', 'Type']].fillna('missing') df.index = df.Manufacturer + '_' + df.Model + '_' + df.Type print(df.index.is_unique) # output True
How to compute the row wise counts of all possible values in an array? # Input: np.random.seed(100) arr = np.random.randint(1,11,size=(6, 10)) arr # output array([[ 9, 9, 4, 8, 8, 1, 5, 3, 6, 3], [ 3, 3, 2, 1, 9, 5, 1, 10, 7, 3], [ 5, 2, 6, 4, 5, 5, 4, 8, 2, 2], [ 8, 8, 1, 3, 10, 10, 4, 3, 6, 9], [ 2, 1, 8, 7, 3, 1, 9, 3, 6, 2], [ 9, 2, 6, 5, 3, 9, 4, 6, 1, 10]]) # Solution def counts_of_all_val..
How to filter every nth row in a dataframe? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution print(df.iloc[::20, :][['Manufacturer', 'Model', 'Type']]) # output Manufacturer Model Type 0 Acura Integra Small 20 Chrysler LeBaron Compact 40 Honda Prelude Sporty 60 Mercury Cougar Midsize 80 Subaru Loyale Small