Python Forum

Full Version: Change a numpy array to a dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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)
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
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?