1

Quick question:

I have the following situation (table): Imported data frame

Now what I would like to achieve is the following (or something in those lines, it does not have to be exactly that)

Goal

I do not want the following columns so I drop them

data.drop(data.columns[[0,5,6]], axis=1,inplace=True)

What I assumed is that the following line of code could solve it, but I am missing something?

pivoted = data.pivot(index=["Intentional homicides and other crimes","Unnamed: 2"],columns='Unnamed: 3', values='Unnamed: 4')

produces

ValueError: Length of passed values is 3395, index implies 2

Difference to the 8 question is that I do not want any aggregation functions, just to leave values as is.

Data can be found at: Data

Noah Weber
  • 312
  • 2
  • 13
  • 3
    Possible duplicate of [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – Georgy Dec 04 '18 at 11:54
  • I suppose it is 8 question of the above, however I edited my question since it does not solve it – Noah Weber Dec 04 '18 at 12:15

1 Answers1

0

The problem with the method pandas.DataFrame.pivot is that it does not handle duplicate values in the index. One way to solve this is to use the function pandas.pivot_table instead.

df = pd.read_csv('Crimes_UN_data.csv', skiprows=[0], encoding='latin1')
cols = list(df.columns)
cols[1] = 'Region'
df.columns = cols
pivoted = pd.pivot_table(df, values='Value', index=['Region', 'Year'], columns='Series', aggfunc=sum)

It should not sum anything, despite the aggfunc argument, but it was throwing pandas.core.base.DataError: No numeric types to aggregate if the argument was not provided.

Jonathan S.
  • 26
  • 1
  • 5