I got the following dataframe:
0
0 Aachen
1 1
2 Valid
3 L5
4 21
5 Fell
6 01/01/1880 12:00:00 AM
7 50.775
8 6.08333
9 (50.775000, 6.083330)
And I want it to look like:
name id nametype recclass mass (g) fall year reclat reclong GeoLocation
Aachen 1 Valid L5 21 Fell 1-1-1880 12:00:00 AM 50.775000 6.083330 (50.775000, 6.083330)
I tried using pivot() an pivot_table() in pandas, but I always get a KeyError:
pv_df = df.pivot_table(index=None, columns='0')
Traceback (most recent call last):
File "/home/alaaeddine/PycharmProjects/test/expectations.py", line 18, in <module>
pv_df = df.pivot_table(index=None, columns='0')
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 5303, in pivot_table
margins_name=margins_name)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/reshape/pivot.py", line 86, in pivot_table
grouped = data.groupby(keys, observed=False)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 6665, in groupby
observed=observed, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/groupby.py", line 2152, in groupby
return klass(obj, by, **kwds)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/groupby.py", line 599, in __init__
mutated=self.mutated)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/groupby.py", line 3291, in _get_grouper
raise KeyError(gpr)
KeyError: '0'
Also tried to set the index and stack/unstack but it does not seem to work.
Any clues on what I am might be missing/doing worng?
Thanks!
DataFramereally only 10 rows long, or is it this just an example (and you actually have like a 10*N longDataFrame)? In that case a simple.Twon't help, and you'll need topivotafter agroupby.cumcount(), assuming your rows always appear in the same order