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

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 :: Cool codes for Python 
Python :: tkinter minsize 
Python :: difference python list and numpy array 
Python :: create an array with same value python 
Python :: add all string elements in list python 
Python :: pandas sum multiple columns groupby 
Python :: django form password field 
Python :: pandas add character to string 
Python :: edit json file python 
Python :: python generate table 
Python :: how to set chrome options python selenium for a folder 
Python :: opencv grayscale to rgb 
Python :: python conda how to see channels command 
Python :: python hsl to rgb 
Python :: Connecting Kaggle to Google Colab 
Python :: how to install flask 
Python :: converting a csv into python list 
Python :: seaborn hue order 
Python :: remove all occurrences of a character in a list python 
Python :: python extract specific columns from pandas dataframe 
Python :: python code to drop columns from dataframe 
Python :: python get current mouse position 
Python :: django import model from another app 
Python :: django settings module LOGIN_URL 
Python :: python nltk tokenize 
Python :: python remove text between parentheses 
Python :: how to get all the files in a directory in python 
Python :: add self role with discord bot python 
Python :: sort list by attribute python 
Python :: pandas df remove index 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =