Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting single column to multiple columns based on unique values [duplicate]

Tags:

pandas

I have the following pandas.DataFrame with shape (1464, 2):

df = pd.DataFrame()
for name in list('ABCD'):
    temp_df = pd.DataFrame(np.random.randint(0,100,size=(len(date_rng), 1)), columns=['value'], index=date_rng)
    temp_df['name'] = name
    df = df.append(temp_df)

The index column has each data duplicated 4 times: one for each string ('ABCD') in the name column.

The dataframe head and tail look like so:

Head

value   name
2018-01-01  47  A
2018-01-02  22  A
2018-01-03  13  A
2018-01-04  66  A
2018-01-05  19  A 

Tail

    value   name
2018-12-28  32  D
2018-12-29  1   D
2018-12-30  5   D
2018-12-31  50  D
2019-01-01  75  D

I would like to convert this (1464, 2) dataframe to shape (366, 4), such that each of the 4 columns are the 4 unique values in df.name.unique() (i..e A, B, C, D). The values for each column are the respective integers in the df.value column.

The final DataFrame should look something like this:

            A   B   C   D
2018-12-28  32  22  21  4
2018-12-29  1   16  2   12
2018-12-30  5   1   65  26
2018-12-31  50  92  21  75
2019-01-01  75  55  33  34

I am sure there must be a nice reindex function or something of this sort to perform the task efficiently, as opposed to looping and recreating the dataframe.

like image 232
Newskooler Avatar asked Aug 17 '26 07:08

Newskooler


1 Answers

You can use this:

df.pivot(columns='name',values='value')
like image 92
Joe Avatar answered Aug 20 '26 00:08

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!