4
$\begingroup$

I'm trying to create a contour map from two variables which store some temperature values and a third variable which is the time stamp. I used this notebook as a tutorial

https://plot.ly/pandas/contour-plots/

I'm not able to convert the pandas dataframe created, into a 1d array. And the kde_scipy doesn't work with a nd-array. I tried converting the dataframe into a 1d array using .as_matrix() but this is the error I am receiving.

Degrees of freedom <= 0 for slice

How can I convert this CSV file (with 3 columns of data) imported as a dataframe into individual columns of data? Or can I directly import each column of data into a 1d array and use it in the function kde_scipy?

$\endgroup$

2 Answers 2

5
$\begingroup$

You can try this

import pandas as pd
import numpy as np
filename = 'data.csv'
df1 = pd.read_csv(filename)
#convert dataframe to matrix
conv_arr= df1.values

#split matrix into 3 columns each into 1d array

arr1 = np.delete(conv_arr,[1,2],axis=1) 
arr2 = np.delete(conv_arr,[0,2],axis=1) 
arr3 = np.delete(conv_arr,[0,1],axis=1) 

#converting into 1D array
arr1 = arr1.ravel()
arr2 = arr2.ravel()
arr3 = arr3.ravel()

This should mostly do the job. Use the arr1 ,arr2,arr3 in the function you mentioned. They are the 1d array of the columns you split

$\endgroup$
5
$\begingroup$

You can first convert the DataFrame to NumPy format by calling .values, after which the resulting numpy.ndarray has the same dimensions as your original DataFrame. Then, run .flatten() to collapse it into one dimension.

my_dataframe.values.flatten()
$\endgroup$
1
  • 4
    $\begingroup$ .flatten() returns a copy, .ravel() a view. $\endgroup$
    – K3---rnc
    May 12, 2017 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.