Python Forum
Change a numpy array to a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change a numpy array to a dataframe
#1
As anyone can see from the attached screenshot after I scaled a dataframe, it is now a numpyndarray. That would make it hard to save. SO how do I conveit ot a datframe?

The attachment is a screenshot of the python code in question.

How can I change a numpyndarray back to a dataframe?

Any help appreciated.

Respectfully,


Led_Zeppelin

Attached Files

Thumbnail(s)
   
Reply
#2
That was interesting. At first no attachment, then Pop! up it appears.

The screenshot is fairly worthless. Post the code that generates these results. The code provides context for what you are trying to do.

Looking at sklearn.StandardScaler.fit_transform() documentation, it expects the first argument to be an "array-like" object. Did you already convert df to an array before calling fit_transform()?

Making a dataframe from an array is easy. It looks just like making a dataframe from a list. But why do you want to do this? From the tiny bit of code I can see, it looks like you change the fit_transform results to a dataframe just so you can write it to a csv file? That is unnecessary. It is easy to write a csv format file. You don't need pandas or csv to do it. This code writes a csv file using numpy,savetxt()
import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [i * 0.5 for i in range(1, 11)], "B": [i**0.5 for i in range(1, 11)]})
np.savetxt("test.csv", df.values, delimiter=",", header=",".join(df.columns), comments="")
But if you really think you need to convert your results back to a dataframe, that is easy too.
# Do not overwrite df.  You will need it for getting column names
results = scaler.fit_transform(df.values())
df2 = pd.DataFrame(results, index=df.index, columns=df.columns)
noisefloor and Larz60+ like this post
Reply
#3
I am doing it for consistency. At first, I tried to just use
convert to csv. I got the error you see. It is not a dataframe the command fails.

The solution-change to a dataframe. That seem simple and easy. What is wrong with that?

Now, I am using the command

df =pd.Dataframe(locals()[ndarray_name])

I have not converted a numpy array to a dataframe in a while do I forgot the syntax. Do I use locals or globals to make the conversion?

Also, understand that I am just being consistent in saving thing as a csv file.

Help appreciated.

Led_Zeppelin
Reply
#4
Why are you using locals()? You should almost never use locals(). Is using locals() some weird conda notebook thing? If you can get locals()[ndarray_name], it means there is a local variable that references the same object as locals()[ndarray_name). Why not use the variable?
x = "Hello World"
print(x)
print(locals()["x"])
Output:
Hello World Hello World
Your csv file will not have any column names. Is that consistent? I honestly don't know. Do you use csv files as some kind of persistent storage?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 367 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,857 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 547 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Expand the range of a NumPy array? PythonNPC 0 742 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  from numpy array to csv - rounding SchroedingersLion 6 2,152 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,588 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  numpy.array has no attribute head Led_Zeppelin 1 1,225 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,136 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  array change svm 6 1,475 Jun-23-2022, 01:01 PM
Last Post: svm
  go over and search in numpy array faster caro 7 1,732 Jun-20-2022, 04:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020