简体   繁体   中英

How do I shift the contents in the first column into column names in Pandas

I god a dataFrame called df_tags, and I'd like to shift the whole dataframe by using all values of Tag column as column header name with their corresponding values as the first row of values.

I have tried using df_tags.pop suggested here because my dataframe initially was converted from a Series that was initally a dictionary, but it hasn't really been working.

df_tags.head()

           Tag     0
      0    NNP  3966
      1     IN   643
      2     CD   589
      3     DT   434
      4      (   324
      5     NN  4104
      6      :  3271
      7      )   326
      8    VBD   196
      9    VBN    92
      10    RB    77
      11  PRP$    38
      12    JJ  2034
      13     .    96
      14   VBZ   541
      15    CC   285

      ...   ...  ...  # and many many more...

into:

      NNP    IN    CD    DT    (    NN   ...
0     3966   643   589   434   324  4104 ...



try:

df.set_index("Tag").T

Tag   NNP   IN   CD   DT    (    NN     :  ...  VBN  RB  PRP$    JJ   .  VBZ   CC
0    3966  643  589  434  324  4104  3271  ...   92  77    38  2034  96  541  285

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM