0
$\begingroup$

I have pandas data frame

data=df.loc[[0]] 
print(data)
0    3.5257,3.5257,3.5257000000000005,3.5257,3.5257...
Name: testdata, dtype: object

I need to convert it to numpy array and want to plot the figure

$\endgroup$

1 Answer 1

2
$\begingroup$

You can use the .values attribute of the dataframe to convert it to a numpy array, and then use the numpy array to plot the figure. Here's an example:

import matplotlib.pyplot as plt

data = df.loc[[0]].values[0]
plt.plot(data)
plt.show()

Note that in the above code, df.loc[[0]].values[0] is used to extract the numpy array from the dataframe, as the output of df.loc[[0]] is still a dataframe. You can also use the .to_numpy() method to convert the dataframe to numpy array.

data = df.to_numpy()
plt.plot(data)
plt.show()

Also make sure that you have the matplotlib library imported and the data is of numerical values, otherwise you will get an error while plotting.

$\endgroup$
2
  • $\begingroup$ Thanks @Rezuana $\endgroup$ Commented Jan 27, 2023 at 15:41
  • $\begingroup$ my data comes something like this '3.5257,3.5257,3.5257,3.5257,3.5257,3.5257' after using data = df.loc[[0]].values[0] and the plt.show() gives the blank plot $\endgroup$ Commented Jan 28, 2023 at 6:46

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.