Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert numpy array to dataframe

import numpy as np
import pandas as pd

my_array = np.array([[11,22,33],[44,55,66]])

df = pd.DataFrame(my_array, columns = ['Column_A','Column_B','Column_C'])

print(df)
print(type(df))
Comment

df to np array

df = pd.DataFrame({"A": [1, 2], "B": [3.0, 4.5]})
>>> df.to_numpy()
array([[1. , 3. ],
       [2. , 4.5]])
Comment

make pandas df from np array

numpy_data = np.array([[1, 2], [3, 4]])
df = pd.DataFrame(data=numpy_data, index=["row1", "row2"], columns=["column1", "column2"])
print(df)
>>>
  column1  column2
row1        1        2
row2        3        4
Comment

convert a pandas df to a numpy array

x = df.to_numpy()
x
Comment

numpy arrauy to df

numpy_data = np.array([[1, 2], [3, 4]])
df = pd.DataFrame(data=numpy_data, index=["row1", "row2"], columns=["column1", "column2"])
print(df)
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 :: cv2 save video mp4 
Python :: tkinter maximum window size 
Python :: array for each in python 
Python :: np array value count 
Python :: what happen when we apply * before list in python 
Python :: how to load ui file in pyqt5 
Python :: set window size tkinter 
Python :: increase limit of recusrion python 
Python :: disable devtools listening on ws://127.0.0.1 python 
Python :: python print code 
Python :: how to make a discord bot delete messages python 
Python :: convert grayscale to rgb python 
Python :: how to remove all spaces from a string in python 
Python :: python console animation 
Python :: email validation python 
Python :: how to do pandas profiling 
Python :: django integer field example 
Python :: Print Table Using While Loop In Python 
Python :: how to take list of float as input in python 
Python :: sklearn mean square error 
Python :: tkinter info box 
Python :: triangle pygame 
Python :: draw bounding box on image python cv2 
Python :: godot code for movement for topdown game 
Python :: how to find the lowest value in a nested list python 
Python :: python flask replit 
Python :: how to get all file names in directory python 
Python :: python create n*n matrix 
Python :: nodemon python 
Python :: mnist fashion dataset 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =