Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to make table
#1
Hi,
I have data as below:
name category date score
Z    s1       2020-02-12    
A    s1       2020-02-12
B    s2       2020-01-23
U    s1       2020-02-06
A    s2	      2020-01-26
B    s3       2020-02-10
I want to define a table based on unique names(as rows) & unique category as column names. I sue below code, I could not be able to name the rows and columns.

import pandas as pd
df = pd.read_csv(r'D:\PythonCodes\tmpdata.txt', delimiter= '\s+', index_col=False)
df['category'].value_counts()
df2=df.groupby(['name','date','category']).agg({'score': lambda x: x.mean() * 100.0})
df2.to_csv('out.csv',index=True)
myrws=sorted(list(df.name.unique()))
mycols=sorted(list(df.category.unique()))
lenrows=len(myrws)
for i in range (len(myrws)):
    tmp=myrws[i]
    print(tmp)
    
import numpy as np
d = np.zeros((len(myrws),len(mycols)))
d[:] = np.nan

I am getting the output :
Out[15]:
array([[nan, nan, nan],
[nan, nan, nan],
[nan, nan, nan],
[nan, nan, nan]])
I want to convert to dataframe. and name the rows and columns as:
Name s1  s2  s3
A    nan nan nan
B    nan nan nan
U    nan nan nan
Z    nan nan nan
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Match a table with sub-total values to its detail value table klllmmm 2 1,658 Apr-03-2019, 11:28 AM
Last Post: klllmmm

Forum Jump:


Users browsing this thread: 1 Guest(s)