0

I would like to take a given column and use its values as (multiple) new columns in a data frame. It looks like a pivot but without index.

If I have a data frame such as:

df = pd.DataFrame({"name": [l for l in "AAABBB"], "val": np.random.random(6)})

Example data:

  name       val
0    A  0.600525
1    A  0.316421
2    A  0.744816
3    B  0.818978
4    B  0.647259
5    B  0.455111

I would like to group by the column "name" but I do not want to aggregate the results. I want to have a new column A and a new column B that contain the values in column "val".

name         A         B
0     0.600525  0.818978
1     0.316421  0.647259
2     0.744816  0.455111

The index doesn't matter here and of course, the number of values in A and B is not the same.

smci
  • 32,567
  • 20
  • 113
  • 146
Ger
  • 9,076
  • 10
  • 37
  • 48
  • Check Q and A question 10 – BENY Dec 18 '19 at 22:41
  • I am not sure this is the same question. I do not want to count the values of A or B. I want the values in column val. The question 10 is effectively the same, but the answer do a cumulative sum. – Ger Dec 18 '19 at 22:48
  • that is cumcount not cumsum . – BENY Dec 18 '19 at 22:49
  • Yes but I neither want to count nor to sum. I would like the values without any operation. – Ger Dec 18 '19 at 22:51
  • 1
    You just miss the index , cumcount is to create the index for each name – BENY Dec 18 '19 at 22:52
  • Ah ! Ok, I understand. Do you have the possibility to complete the answer of question 10 to give a complete solution ? – Ger Dec 19 '19 at 08:13
  • I will contact Pir to include that – BENY Dec 19 '19 at 13:29
  • If you say *the number of values in A and B is not the same*, then the example should reflect that, e.g. `df = pd.DataFrame({"name": [l for l in "AAABBBBB"], ...`. You're not doing *aggregate* and you're not doing *filter values on a given column* and that's not what 'groupby` does anyway: you mean to say *"pivot, using column `name` to make new frame's columns"*. Best not to reference so many keywords to explain what you're *not* doing - this question unwantedly matched searches for all sorts of unrelated queries, e.g. *[pandas] name result of aggregate* – smci Mar 09 '20 at 05:44

0 Answers0