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 :: get last 3 things in a list python 
Python :: get last 3 in array python 
Python :: list comprehension if elseif 
Python :: how do you change a string to only uppercase in python 
Python :: find all regex matches python 
Python :: jupyter dark theme vampire 
Python :: how to replace the last character of a string in python 
Python :: python dunder 
Python :: check if numpy array contains only duplicates 
Python :: python iterate through objects attributes 
Python :: python remove blanks from string 
Python :: pd.merge remove duplicate columns 
Python :: reverse a string python 
Python :: how to add a value to a list in python 
Python :: flask error handling 
Python :: pandas dataframe.to_dict 
Python :: df empty python 
Python :: queue using linked list in python 
Python :: Upper letter list 
Python :: install anaconda python 2.7 and 3.6 
Python :: df to sql mysql 
Python :: Converting categorical feature in to numerical features using target ordinary encoding 
Python :: Pandas: How to Drop Rows that Contain a Specific String in 2 columns 
Python :: how to print horizontally in python 
Python :: python minigame 
Python :: remove leading and lagging spaces dataframe python 
Python :: how to print a string in python 
Python :: how to make a loading gif in pyqt5 
Python :: dfs python 
Python :: python shuffle 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =