How to group by column values into index?

helpme

I have dataframe that looks like this

Date    group   value
1997    a       num
1997    b       num
1997    c       num
1997    d       num
1997    e       num
1997    f       num
1997    g       num
1998    a       num
1998    b       num
1998    c       num
1998    d       num
1998    e       num
1998    f       num
1998    g       num
1999    a       num

How do I make it look like this

index   group   1997    1998    1999    2000    2001    2002
0       a       num     num     num     num     num     num
1       b       num     num     num     num     num     num
2       c       num     num     num     num     num     num
3       d       num     num     num     num     num     num
4       e       num     num     num     num     num     num
5       f       num     num     num     num     num     num
6       g       num     num     num     num     num     num
wjandrea

Make a pivot table.

>>> df.pivot_table(columns='Date', values='value', index='group', aggfunc='first')
Date  1997 1998 1999
group               
a      num  num  num
b      num  num  NaN
c      num  num  NaN
d      num  num  NaN
e      num  num  NaN
f      num  num  NaN
g      num  num  NaN

(Here data is missing since I'm only using the input data you provided.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to swap a group of column headings with their values in Pandas

How to group by column and copy all values of a group to one row in pandas?

How to include a column in a group by without unwanted values

How to group by two column with swapped values in pandas?

How to group same column values together with SQL?

How to index rows with duplicate values in a column?

How to count ratios of two column values by group?

How to group by column and drop or separate values in pandas?

How to find 2 largest values from group of rows in multiple columns in Python and also show its row and column index at output

How to count values of a column and group by range?

How to increment index per column / group

How to group similar column values in scala?

How to group row names by values of a column

How to Group By Column Values?

How to decode json, group by a column value, and sum values in each group?

How to find 2 largest values from group of rows in multiple columns in Python and also show its row and column index without duplicates

How to group subset data by irregular column values

How to combine values in a column by group?

how to get index of first occurence of group in a column?

how to order by and group by equal column values in SQL?

How to get the index of multiple mininum values in a column?

Group by and return all index values where a substring of text exists in a column

Pandas: How to set index to a column value, group columns, and return values

How to concatenate column values from each group

How to set group by column-name as column instead of index?(Pandas)

How to change column values based on value of an index

How to group row values depending on another column?

How to keep the values of one column per index?

How to get values based on column index in pandas?