Hi,
I have data as below:
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.
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:
I have data as below:
1 2 3 4 5 6 7 |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 |
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:
1 2 3 4 5 |
Name s1 s2 s3 A nan nan nan B nan nan nan U nan nan nan Z nan nan nan |