목록전체 글 (462)
Note
How to set the number of rows and columns displayed in the output? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution pd.set_option('display.max_columns', 10) pd.set_option('display.max_rows', 10)
How to find the most frequent value in a numpy array? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # Solution: vals, counts = np.unique(iris[:, 2], return_counts=True) print(vals[np.argmax(counts)]) # output b'1.5'
How to change the order of columns of a dataframe? # Input df = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde')) # 1 df[list('cbade')] # 2 - No hard coding def switch_columns(df, col1=None, col2=None): colnames = df.columns.tolist() i1, i2 = colnames.index(col1), colnames.index(col2) colnames[i2], colnames[i1] = colnames[i1], colnames[i2] return df[colnames] df1 = switch_columns..
How to sort a 2D array by a column # Sort by column position 0: SepalLength print(iris[iris[:,0].argsort()][:20]) [[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa'] [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa'] [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa'] [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa'] [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa'] [b'4.6' b'3.6' b'1.0' b'0.2' b'Iris-setosa'] [b'4.6' b'3.1..
How to select a specific column from a dataframe as a dataframe instead of a series? # Input df = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde')) # Solution type(df[['a']]) type(df.loc[:, ['a']]) type(df.iloc[:, [0]]) # Alternately the following returns a Series type(df.a) type(df['a']) type(df.loc[:, 'a']) type(df.iloc[:, 1])