Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas series to numpy array

ser = pd.Series(pd.Categorical(['a', 'b', 'a']))
>>> ser.to_numpy()
array(['a', 'b', 'a'], dtype=object)
Comment

convert a pandas df to a numpy array

x = df.to_numpy()
x
Comment

panda dataframe to numpy matrix

# Imports
import numpy as np
import pandas as pd


df= pd.read_csv('/content/drive/MyDrive/Colab Notebooks/res_data/temp/file.txt', header = None, delim_whitespace=True, error_bad_lines=False).to_numpy()
df=df.astype(float)  # convert number to float for matric calculations

print(df.shape)  # .... (16, 1) initial shaope
df.resize(4, 4)

print(df)

[[ 1.  2.  3.  4.]
 [ 5.  6.  7.  8.]
 [ 9.  3.  5.  6.]
 [ 8.  9. 79.  0.]]
 
Comment

PREVIOUS NEXT
Code Example
Python :: python data frame check if any nan value present 
Python :: star pattern in python 
Python :: sort the dictionary in python 
Python :: how to change role permissions in discord.py 
Python :: how to add subplots for histogram 
Python :: taking string input from user in python with try except 
Python :: how to compare two text files in python 
Python :: join two dictionaries python 
Python :: np.hstack 
Python :: export pythonpath linux 
Python :: convert pandas column type 
Python :: pygame how to get surface lenght 
Python :: make sure text is on page selenium python 
Python :: python how to change an element in a multi dimensional list 
Python :: how to generate random normal number in python 
Python :: python one line if else 
Python :: python remove form list 
Python :: save dictionary to file numpy 
Python :: comparing two dataframe columns 
Python :: convex hull algorithm python 
Python :: read json from api python 
Python :: multirow np.rand.randint 
Python :: remove comments from python file 
Python :: python change column order in dataframe 
Python :: spacy ner 
Python :: python sqlite insert 
Python :: remove all whitespace from string python 
Python :: sum of number digits python 
Python :: python while not 
Python :: shutil copyfile python 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =