목록Note (468)
Note
How to import only specified columns from a csv file? df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv', usecols=['crim', 'medv']) print(df.head()) # output crim medv 0 0.00632 24.0 1 0.02731 21.6 2 0.02729 34.7 3 0.03237 33.4 4 0.06905 36.2
How to drop rows that contain a missing value from a numpy array? # Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # Solution # No direct numpy function for this. # 1: any_nan_in_row = np.array([~np...
How to create a dataframe with rows as strides from a given series? L = pd.Series(range(15)) def gen_strides(a, stride_len=5, window_len=5): n_strides = ((a.size-window_len)//stride_len) + 1 return np.array([a[s:(s+window_len)] for s in np.arange(0, a.size, stride_len)[:n_strides]]) gen_strides(L, stride_len=2, window_len=4) # output array([[ 0, 1, 2, 3], [ 2, 3, 4, 5], [ 4, 5, 6, 7], [ 6, 7, 8,..
How to change column values when importing csv to a dataframe? # 1: Using converter parameter df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv', converters={'medv': lambda x: 'High' if float(x) > 25 else 'Low'}) # 2: Using csv reader import csv with open('BostonHousing.csv', 'r') as f: reader = csv.reader(f) out = [] for i, row in enumerate(reader): i..
How to filter a numpy array based on two or more conditions? # Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) # Solution condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0) iris_2d[condition] # output array([[ 4.8, 3.4, 1.6, 0.2], [ 4.8, 3.4, 1.9, 0.2], [ 4.7, 3.2, 1.6, 0.2..