Home > Back-end >  Is there a way to create a dataframe without using pd.DataFrame() function?
Is there a way to create a dataframe without using pd.DataFrame() function?

Time:05-21

I have a 2D numpy array that needs to convert into Pandas dataframe type. However, I am not allow to use pd.Dataframe() function. Is there another a way to achieve that?

The array looks sth like this: [("Thu Feb 27 17:23:55", 276, 67140),("Sat Feb 29 05:47:36", 376, 54980)]

array.dtype will return: [("Date", "<U30"), ("ID", "<U30"), ("Payment", "<f8")]

CodePudding user response:

Well not sure this applies, but you can create it starting with a Series I guess:

import pandas as pd

df = pd.Series(arr[:, 0]).to_frame()
df[['col2', 'col3']] = arr[:, 1:]
df.columns = ['Date', 'ID', 'Payment']

Of course this will still use pd.DataFrame under the hood via the to_frame() method.

  • Related