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
#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


Messages In This Thread
RE: Change a numpy array to a dataframe - by deanhystad - Jan-26-2023, 07:01 PM

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