简体   繁体   中英

How to create a certain dataframe based on an Excel-sheet?

I am working with google or tools and in one of the examples a data structure is given. I would like to import this data structure based on an Excel sheet.

This is the given data structure:

jobs = [[[(3, 0), (1, 1), (5, 2)], 
         [(2, 0), (4, 1), (6, 2)],
         [(2, 0), (3, 1), (1, 2)]],
        [[(2, 0), (3, 1), (4, 2)], 
         [(1, 0), (5, 1), (4, 2)],
         [(2, 0), (1, 1), (4, 2)]], 
        [[(2, 0), (1, 1), (4, 2)],
         [(2, 0), (3, 1), (4, 2)],
         [(3, 0), (1, 1), (5, 2)]]]

What I like to do is to import jobs based on an Excel sheet with data given as:

Job Task    M1  M2  M3
1    1      3   1   5
1    2      2   4   6
1    3      2   3   1
2    1      2   3   4
2    2      2   5   4
2    3      2   1   4
3    1      2   3   4
3    2      3   1   5

You should reorganize all your data, grouping by Job. For example:

import pandas as pd

df = pd.read_excel('bb.xlsx')

jobs = set(df['Job'])      #remove duplicates
result = [[[ (df['M1'][i],0), (df['M2'][i],1), (df['M3'][i],2) ] for i in df.index if df['Job'][i] == job] for job in jobs]
print(result)

WARNING: the result is not exactly what you wrote. I think you mispelled some data. Tell me if I am wrong.

I give another answer, using pandas' groupby API:

import pandas as pd

df = pd.read_excel('bb.xlsx')

result = [[[ (row['M1'],0), (row['M2'],1), (row['M3'],2) ] for idx, row in grpdf.iterrows()] for grpname, grpdf in df.groupby('Job')]
print(result)

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