mirror of
				https://github.com/sahinakkaya/til.git
				synced 2025-07-17 14:01:49 +03:00 
			
		
		
		
	TIL: Converting dataframe column to numpy array
This commit is contained in:
		
							
								
								
									
										20
									
								
								python/converting-dataframe-column-to-numpy-array.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								python/converting-dataframe-column-to-numpy-array.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | |||||||
|  | This returns a numpy array: | ||||||
|  |  | ||||||
|  | ```python | ||||||
|  | arr = df["cluster"].to_numpy() | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | ----- | ||||||
|  |  | ||||||
|  | This returns a numpy array of **unique** values: | ||||||
|  |  | ||||||
|  | ```python | ||||||
|  | unique_arr = df["cluster"].unique() | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | You can also use numpy to get the unique values, although there are differences between the two methods([`df.unique`](pandas.pydata.org/pandas-docs/stable/reference/api/pandas.unique.html) doesn't sort while [`np.unique`](numpy.org/devdocs/reference/generated/numpy.unique.html) does): | ||||||
|  |  | ||||||
|  | ```python | ||||||
|  | arr = df["cluster"].to_numpy() | ||||||
|  | unique_arr = np.unique(arr) | ||||||
|  | ``` | ||||||
		Reference in New Issue
	
	Block a user
	 Asocia
					Asocia