1

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!

5
  • 1
    You can always transpose the dataframe using df.T. How did you create the dataframe? May be the problem can be solved easily by fixing that. Commented Jan 30, 2019 at 18:56
  • Is your DataFrame really only 10 rows long, or is it this just an example (and you actually have like a 10*N long DataFrame)? In that case a simple .T won't help, and you'll need to pivot after a groupby.cumcount(), assuming your rows always appear in the same order Commented Jan 30, 2019 at 19:15
  • @ALollz indeed this is a sample and not the big dataframe. My idea was as follows: - load a csv in a dataset. - convert every row of the dataset into dataframe. - test the dataframe agaist some expectations. The way I found to create a dataframe form a dataset row is dt.values[loop_index]. This creates the dataframe I posted in my question. Commented Jan 30, 2019 at 19:21
  • So you just have one long column, and you need to take every 10 rows and pivot them to a single row with 10 columns? Commented Jan 30, 2019 at 19:22
  • 1
    @ALollz Yes. Every 10 rows should be converted in a single row with 10 columns. May be I'm missing a much easier way to do it. But since I'm new to Pandas, I could not find something more simple. Commented Jan 30, 2019 at 19:26

4 Answers 4

3

What you need is not to pivot, but to transpose the dataframe. Here is the documentation.

df = df.transpose()
Sign up to request clarification or add additional context in comments.

6 Comments

Or to be more pythonic: df = df.T
Or, even simpler, df.T
Would that match with OP's output? Have you tried it?
df.transpose output (does not seem to work): ``` <bound method DataFrame.transpose of 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)> ```
Don't forget to actually call it. df.transpose(). The () are important.
|
1

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))

Comments

0

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)

Comments

0

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) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.