Creating Pandas Dataframe between two Numpy arrays, then draw scatter plot Creating Pandas Dataframe between two Numpy arrays, then draw scatter plot python python

Creating Pandas Dataframe between two Numpy arrays, then draw scatter plot


There are a number of ways to create DataFrames. Given 1-dimensional column vectors, you can create a DataFrame by passing it a dict whose keys are column names and whose values are the 1-dimensional column vectors:

import numpy as npimport pandas as pdx = np.random.randn(5)y = np.sin(x)df = pd.DataFrame({'x':x, 'y':y})df.plot('x', 'y', kind='scatter')


Complementing, you can use pandas Series, but the DataFrame must have been created.

import numpy as npimport pandas as pdx = np.linspace(0,2*np.pi)y = np.sin(x)#df = pd.DataFrame()#df['X'] = pd.Series(x)#df['Y'] = pd.Series(y)# You can MIXdf = pd.DataFrame({'X':x})df['Y'] = pd.Series(y) df.plot('X', 'Y', kind='scatter')

This is another way that might help

import numpy as npimport pandas as pdx = np.linspace(0,2*np.pi)y = np.sin(x)df = pd.DataFrame(data=np.column_stack((x,y)),columns=['X','Y'])

And also, I find the examples from karlijn (DatacCamp) very helpful

import numpy as npimport pandas as pdTAB = np.array([[''     ,'Col1','Col2'],                 ['Row1' ,   1  ,   2  ],                 ['Row2' ,   3  ,   4  ],                 ['Row3' ,   5 ,   6  ]])dados = TAB[1:,1:]linhas = TAB[1:,0]colunas = TAB[0,1:]DF = pd.DataFrame(    data=dados,    index=linhas,    columns=colunas)print('\nDataFrame:', DF)


In order to do what you want, I wouldn't use the DataFrame plotting methods. I'm also a former experimental physicist, and based on experience with ROOT I think that the Python analog you want is best accomplished using matplotlib. In matplotlib.pyplot there is a method, hist2d(), which will give you the kind of heat map you're looking for.

As for creating the dataframe, an easy way to do it is:

df=pd.DataFrame({'x':x, 'y':y})