简体   繁体   中英

How to Convert the Numpy array to a DataFrame?

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

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)'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM