2

I'm trying to create a new columns on headerless data and want to find a workable solution.

This is the data I'm working with - it is public.

    {A42E2F04-2538-4A25-94C5-49E29C6C8FA8}  18500   1995-01-31 00:00    TQ1 1RY F   N   L   VILLA PARADISO  FLAT 10 HIGHER WARBERRY ROAD    TORQUAY TORQUAY.1   TORBAY  TORBAY.1    A   A.1
0   {1BA349E3-2579-40D6-999E-49E2A25D2284}  73450   1995-10-09 00:00    L26 7XJ D   Y   F   6   NaN CATKIN ROAD LIVERPOOL   LIVERPOOL   KNOWSLEY    MERSEYSIDE  A   A
1   {E5B50DCB-BC7A-4E54-B167-49E2A6B4148B}  59000   1995-03-31 00:00    BH12 2AE    D   N   F   28  NaN ALDER ROAD  POOLE   POOLE   POOLE   POOLE   A   A
2   {81E50116-D675-4B7F-9F8D-49E2B5D43271}  31000   1995-12-04 00:00    IP13 0DR    D   Y   F   NONSUCH COTTAGE NaN THE STREET  HACHESTON   WOODBRIDGE  SUFFOLK COASTAL SUFFOLK A   A
3   {B97455B9-75CB-40BB-A615-42C53683E143}  95000   1995-09-22 00:00    WS14 0BE    D   N   F   FOX COVER COTTAGE   NaN HALL LANE   LICHFIELD   LICHFIELD   LICHFIELD   STAFFORDSHIRE   A   A
4   {F0D1E8DA-C00D-467A-A41C-42C5378DB6E0}  45450   1995-02-28 00:00    S42 5GA S   Y   F   109 NaN ELVASTON ROAD   NORTH WINGFIELD CHESTERFIELD    NORTH EAST DERBYSHIRE   DERBYSHIRE  A   A

It has no header.

I've built one thus:

    columns = ["Transaction id", "Price", "Date of Transfer", "Postcode","Property Type",
    "PAON", "SAON", "Street", "Locality", "Town/City", "District", "County",
           "PPD Category Type"]
s = pd.DataFrame(columns).T
s

0   1   2   3   4   5   6   7   8   9   10  11  12
0   Transaction id  Price   Date of Transfer    Postcode    Property Type   PAON    SAON    Street  Locality    Town/City   District    County  PPD Category Type

I thought I could concat the two datasets, which I have done, but can't work out how to switch column data to top of dataframe without losing the current columns index which is actually data.

My code to combine the two frames:

pd.concat([s, df_price_paid.head()], axis=1)

Results in:

    0   1   2   3   4   5   6   7   8   9   ... L   VILLA PARADISO  FLAT 10 HIGHER WARBERRY ROAD    TORQUAY TORQUAY.1   TORBAY  TORBAY.1    A   A.1
0   Transaction id  Price   Date of Transfer    Postcode    Property Type   PAON    SAON    Street  Locality    Town/City   ... F   6   NaN CATKIN ROAD LIVERPOOL   LIVERPOOL   KNOWSLEY    MERSEYSIDE  A   A
1   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... F   28  NaN ALDER ROAD  POOLE   POOLE   POOLE   POOLE   A   A
2   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... F   NONSUCH COTTAGE NaN THE STREET  HACHESTON   WOODBRIDGE  SUFFOLK COASTAL SUFFOLK A   A
3   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... F   FOX COVER COTTAGE   NaN HALL LANE   LICHFIELD   LICHFIELD   LICHFIELD   STAFFORDSHIRE   A   A
4   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... F   109 NaN ELVASTON ROAD

What do I do?

elksie5000
  • 7,084
  • 12
  • 57
  • 87
  • 1
    Does this answer your question? [Renaming column names in Pandas](https://stackoverflow.com/questions/11346283/renaming-column-names-in-pandas) – Michael Delgado Aug 07 '22 at 07:00
  • 2
    If the first `N` rows of csv file is empty, then you can add parameter `skip_rows=N` to the `read_csv` – Nuri Taş Aug 07 '22 at 07:00
  • 2
    well - if the csv doesn't have a header row, then pass `header=None, names=list_of_column_names`. Read the [`pd.read_csv`](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html) docs. they're helpful. – Michael Delgado Aug 07 '22 at 07:03
  • 1
    Is this being added in via a CSV, XLSX or another format? – hlin03 Aug 07 '22 at 07:14
  • 1
    you can just do `df_price_paid.columns = columns` where `columns` on the right side of the `=` is the list containing the names of your columns – 99_m4n Aug 07 '22 at 09:07
  • If your dataframe source has no header row, then create df_price_paid after creating the columns list and add columns=columns to the DataFrame parameters – Ze'ev Ben-Tsvi Aug 07 '22 at 11:09

0 Answers0