Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

np array to df

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

convert array to dataframe python

np.random.seed(123)
e = np.random.normal(size=10)  
dataframe=pd.DataFrame(e, columns=['a']) 
print (dataframe)
          a
0 -1.085631
1  0.997345
2  0.282978
3 -1.506295
4 -0.578600
5  1.651437
6 -2.426679
7 -0.428913
8  1.265936
9 -0.866740

e_dataframe=pd.DataFrame({'a':e}) 
print (e_dataframe)
          a
0 -1.085631
1  0.997345
2  0.282978
3 -1.506295
4 -0.578600
5  1.651437
6 -2.426679
7 -0.428913
8  1.265936
9 -0.866740
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

convert pandas dataframe to numpy dataframe

DataFrame.to_numpy(dtype=None, copy=False, na_value=<object object>)
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 :: OSError: cannot write mode RGBA as JPEG Python 
Python :: list of prime numbers in python 
Python :: how to change column type to string in pandas 
Python :: python import from other folder outside folder 
Python :: dollar 
Python :: make dataframe from list of tuples 
Python :: rename multiple pandas columns with list 
Python :: python discord webhook 
Python :: how to refresh windows 10 with python 
Python :: ckeditor django 
Python :: debugging pytest in vscode 
Python :: python install package from code 
Python :: exclude columns pandas 
Python :: find duplicated rows with respect to multiple columns pandas 
Python :: tkinter python may not be configured for Tk 
Python :: get size of window tkinter 
Python :: spark dataframe get unique values 
Python :: how to find if a value is even or odd in python 
Python :: pydrive list folders 
Python :: api xml response to json python 
Python :: sort python dictionary by date 
Python :: install python 3.6 mac brew 
Python :: generate matrix python 
Python :: calculate euclidian distance python 
Python :: mouse in pygame 
Python :: tkinter window to start maximized 
Python :: get all file names in a folder python 
Python :: flask getting started 
Python :: how to know if python is 64 or 32 bit 
Python :: python pygame key input 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =