Reputation: 59
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!
Upvotes: 1
Views: 8048
Reputation: 59
Thanks for your help guys, I could make work using the following code:
import pandas as pd
#create the dataset
dt = pd.read_csv('/path/to/file.csv')
# extract the first row in a new dataframe
df = pd.DataFrame(dt.values[0])
# transpose the new dataframe
df = df.transpose()
# rename the columns of the new dataframe
df.columns = ['name', 'id', 'nametype', 'recclass', 'mass (g)', 'fall', 'year', 'reclat', 'reclong', 'GeoLocation']
This is the output:
name id nametype recclass mass (g) fall year reclat \
0 Aachen 1 Valid L5 21 Fell 01/01/1880 12:00:00 AM 50.775
reclong GeoLocation
0 6.08333 (50.775000, 6.083330)
Upvotes: 0
Reputation: 8641
You need:
df = pd.DataFrame(data.T)
df.columns = ['name', 'id', 'nametype', 'recclass', 'mass (g)', 'fall', 'year', 'reclat', 'reclong', 'GeoLocation']
Output:
name id nametype recclass mass (g) fall year reclat reclong GeoLocation
0 Aachen 1 Valid L5 21 Fell 01/01/1880 12:00:00 AM 50.775 6.08333 (50.775000, 6.083330)
Upvotes: 0
Reputation: 1844
You can use the reshape tool to do this. In this case you will want to make sure to access the values of DataFrame, and reshape it so that it is 1 row and as many columns as needed. To do this, you will need to use the following code below:
pv_df = pd.DataFrame(df.values.reshape(1,-1))
Upvotes: 1
Reputation: 353
What you need is not to pivot, but to transpose the dataframe. Here is the documentation.
df = df.transpose()
Upvotes: 3