1

I'm totally new to python/pandas so please bear with me.

I have been trying for what seems like an eternity to rearrange my dataframe (see the numbers beside in/out...). I have read several answers to what seem like similar questions on stack overflow but I can't get anything to work for me.

Initially I had a table that looked like this:intial table

I want to make it look like this (but with the actual dates and values):

company--date--date--date--date

ebay--------val-----val---val----val

amazon----val-----val---val----val

Initially I thought I could use df.pivot but then I didn't know what to put for index= because I didn't have a name for the columns. So I figured out how to give them a name using

df.columns.names = 'Company'

Now I have a table like this:table as it is now

Based on other answers I've seen on this site I tried again to use df.pivot 1.with index = 'Company' and I get KeyError: 'Company' 2.also with index = ['Company'] also get KeyError: 'Company'

Can anyone tell me what it is that I'm doing wrong?

Thanks in advance for your help

Karn Kumar
  • 8,518
  • 3
  • 27
  • 53
Helen
  • 63
  • 6
  • dont use links to pictures please. – Christian Sloper Nov 13 '18 at 17:31
  • It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here. – Helen Nov 13 '18 at 17:33
  • https://stackoverflow.com/q/47152691/6361531 – Scott Boston Nov 13 '18 at 17:33
  • 1
    You're passing 'Company' as the name of the column _index_, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use `print(df.columns)` or `df.columns = ['Col1','Col2']` – G. Anderson Nov 13 '18 at 17:37
  • @G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying `df.pivot(index='Company')` I would achieve that. Am I totally misunderstanding something here? – Helen Nov 13 '18 at 17:58
  • @Helen, better you can try as `df.set_index('Company')` if you want Company as an index though. – Karn Kumar Nov 13 '18 at 18:01
  • Changing the `.name` attribute of an element doesn't do the same thing as changing the element itself. If you want to change the actual index, you can use `pd.set_index()`. However, that won't do exactly what you're trying to do. In one of the answers below, @jeevan kumar shows a solution using `transpose` – G. Anderson Nov 13 '18 at 18:01
  • You can change the index using set_index however,there is better way with transpose (df.T) method in pandas that's mend for it as already explained :-) – Karn Kumar Nov 13 '18 at 18:07

1 Answers1

1

Think you mean 'transpose' not 'pivot'. Can you please try doing the following? df1.T. Full example below from pandas.DataFrame.transpose

setup:

d1 = {'col1': [1, 2], 'col2': [3, 4]}
df1 = pd.DataFrame(data=d1)
df1

output:

   col1  col2
0     1     3
1     2     4

code:

df1.T # or df1.transpose()

output:

      0  1
col1  1  2
col2  3  4
jeevs
  • 131
  • 5