Home > Mobile >  How to Convert the Numpy array to a DataFrame?
How to Convert the Numpy array to a DataFrame?

Time:09-03

I have to convert this numpy array to a dataframe

array([[['ID                                     0x4501'],
        ['Delivery_person_ID                   S13DEL02'],
        ['Delivery_person_Age                   21.0000'],
        ...,
        ['City                                    Urban'],
        ['Time_taken (min)                      24.0000'],
        ['Name: 0, dtype: object']],

       [['ID                                     0xb329'],
        ['Delivery_person_ID                  ES18DEL02'],
        ['Delivery_person_Age                 32.000000'],
        ...,
        ['City                            Metropolitian'],
        ['Time_taken (min)                    33.000000'],
        ['Name: 1, dtype: object']]], dtype=object)

the output should look like this

ID     Delivery_person_ID  Delivery_person_Age ... City      Time_taken
0x4607 S13DEL02             21.0000                Urban      24.0000
0xb329 ES18DEL02            32.0000             Metropolitian 33.000000

CodePudding user response:

Can be done like this:

data = {}
for item in my_array.ravel():
    if 'dtype: object' in item:
        continue
    parts = item.split()
    key = ' '.join(parts[:-1])
    value = parts[-1]

    try:
        data[key].append(value)
    except KeyError:
        data[key] = [value]

df = pd.DataFrame(data)

df['Delivery_person_Age'] = pd.to_numeric(df['Delivery_person_Age'])
df['Time_taken (min)'] = pd.to_numeric(df['Time_taken (min)'])
  • Related