2

I read data from an excel file, it is like this: Que

It is in a data frame format.I want the modified data frame as below:

Sem1  Sem2   Sem3  
50     36     28

45     41     47

50     48     43

I have used below code:

for f in lparameters: print("Creating Column:" ,f) df_temp[f]=dfs["Total"].get_group(f)

But this code the only one (first column).

Please help me out !!

1 Answer 1

1

You can use set_index with cumcount and reshape by unstack:

df = df.set_index([df.groupby('Sem').cumcount(), 'Sem'])['Total'].unstack()
print (df)
Sem  Sem2  Sem3  Seml
0      36    28    50
1      41    47    45
2      48    43    50

Another solution is create list per group and then use DataFrame constructor:

a = df.groupby('Sem')['Total'].apply(list)
df = pd.DataFrame(a.values.tolist(), index=a.index).T
print (df)
Sem  Sem2  Sem3  Seml
0      36    28    50
1      41    47    45
2      48    43    50
Sign up to request clarification or add additional context in comments.

Comments

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.