如何在Pandas中将单列数据框转换为单行数据框?

时间:2019-01-30 18:49:22

标签: python pandas

我得到以下数据框:

                        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)

我希望它看起来像:

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)

我尝试在熊猫中使用pivot(),pivot_table(),但是我总是收到一个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'

也尝试设置索引和堆栈/非堆栈,但似乎不起作用。

关于我可能正在丢失/正在做的任何线索吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

您所需要的不是旋转,而是转置数据框。 Here是文档。

df = df.transpose()

答案 1 :(得分:0)

您可以使用重塑工具来执行此操作。在这种情况下,您将要确保访问DataFrame的值,并对其进行整形,以使其为1行,并根据需要提供尽可能多的列。为此,您将需要在下面使用以下代码:

pv_df = pd.DataFrame(df.values.reshape(1,-1))

答案 2 :(得分:0)

您需要:

df = pd.DataFrame(data.T)
df.columns = ['name', 'id',  'nametype',    'recclass',    'mass (g)',    'fall',    'year',    'reclat',  'reclong', 'GeoLocation']

输出:

    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)

答案 3 :(得分:0)

感谢您的帮助,我可以使用以下代码进行工作:

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']

这是输出:

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)