1

I have a data with position (row/column),

df = pd.DataFrame({'ID': ['a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'b02', 'b02', 'b02','b02', 'b02', 'b02', 'b02', 'b02'],
                   'Row': [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 3, 3, 3],
                   'Col': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3, 1, 3, 1, 2, 3],
                   'Result': ['p', 'f', 'p', 'p', 'p', 'f', 'p', 'p', 'p', 'p', 'p', 'p', 'f', 'p', 'p', 'p']})

and I am trying to turn it into tables, and calculate how many p and f in the table, like these:

ID: a01
p  f  p
p  p  f
p  p  p

p: 7 f: 2

ID: b02
p     p
p     f
p  p  p

p: 6 f: 1

There are some missing data, but the number of the row and column are fixed, so just leave them blank.

I have no idea how to achieve this, any thought?

2 Answers 2

2

You can do it like this:
I did correct and input error in your input dataframe.
(Changed the value in df.loc[8, 'ID'] from b02 to a01)

df = pd.DataFrame({'ID': ['a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'b02', 'b02','b02', 'b02', 'b02', 'b02', 'b02'],
                   'Row': [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 3, 3, 3],
                   'Col': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3, 1, 3, 1, 2, 3],
                   'Result': ['p', 'f', 'p', 'p', 'p', 'f', 'p', 'p', 'p', 'p', 'p', 'p', 'f', 'p', 'p', 'p']})

df

dfs = {}
for n, g in df.groupby('ID'):
    dfs[n] = g.pivot('Row', 'Col', 'Result').fillna('')
    print(f'ID: {n}')
    print(dfs[n])
    print('\n')
    print(dfs[n].stack().value_counts().to_dict())
    print('\n')

Output:

ID: a01
Col  1  2  3
Row         
1    p  f  p
2    p  p  f
3    p  p  p


{'p': 7, 'f': 2}


ID: b02
Col  1  2  3
Row           
1    p     p
2    p     f
3    p  p  p


{'p': 6, 'f': 1}

Update to add concatenation and output to csv or excel.

pd.concat(dfs, keys=dfs.keys()).to_csv('out.csv')
!type out.csv

Output file contents:

,Row,1,2,3
a01,1,p,f,p
a01,2,p,p,f
a01,3,p,p,p
b02,1,p,,p
b02,2,p,,f
b02,3,p,p,p
Sign up to request clarification or add additional context in comments.

4 Comments

@ Scott Boston - Is there any way to save the result as csv or excel file? I tried dfs.to_csv('Output.csv'), but it doesn't work
Do you want two different excel worksheets one for each dataframe, or would you like to concat the two dataframes together? Yes, we can get the output dataframe(s) to a csv or excel. Just let me know how you'd like to see them?
@ Scott Boston -- I would like to contact the two dataframes together. Thanks
@Joanne Look at update.... I used pd.concat to concatentation the dictionary entries together and use to_csv to create a file, you can use to_excel also.
2

You can do .value_counts on group

dt = df.groupby(['ID'])['Result'].value_counts().rename('count').reset_index()
print(dt)

    ID Result  count
0  a01      p      6
1  a01      f      2
2  b02      p      7
3  b02      f      1

2 Comments

This is helpful, thanks! Do you know how to turn the data into table?
what do you mean by table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.