0

I'm pulling data from Sqlite3 and moving it into a dataframe to work with it. However, I get this weird output where it places the first column name into the second row while the other column names are unaffected in the first row. This creates problems as pandas won't recognize the first row's column name (it only sees column names 2-4). What's the best way to get the column names on the first row?

Example output:

             (x$1000)     PRN AMT TICKER
CUSIP                                   
594918104  3034828140  1612323669   MSFT
037833100  2463247977  2628732382   AAPL
02079K305  2096049986    93429916  GOOGL

Wanted output:

CUSIP       (x$1000)     PRN AMT TICKER
594918104  3034828140  1612323669   MSFT
037833100  2463247977  2628732382   AAPL
02079K305  2096049986    93429916  GOOGL
C_Turbo
  • 139
  • 1
  • 8
  • 2
    CUSIP is in the index `df = df.reset_index()` – Scott Boston Nov 25 '22 at 23:54
  • If you're trying to read from a sqlite db, these links might help: https://stackoverflow.com/questions/36028759/how-to-open-and-convert-sqlite-database-to-pandas-dataframe and https://datacarpentry.org/python-ecology-lesson/09-working-with-sql/index.html – Dash Nov 26 '22 at 00:11
  • @ScottBoston, Thanks for the quick reply. I'm new to working with Pandas so I apologize if this is a dumb question. Do I need to reset the index every time I want to reference the column names? – C_Turbo Nov 26 '22 at 00:12
  • 1
    @CarterTurbett Yes. – Scott Boston Nov 26 '22 at 01:53

1 Answers1

0

Right now you have CUSIP as an index, so it is showing up in this orientation. To add a new index column and remove this columns index attribute, use:

df.reset_index(drop=TRUE)

The drop=TRUE is to avoid the old index being added as a column.

imad97
  • 224
  • 1
  • 8