0

Pandas is not labelling columns correctly and it does make sense because I have used this method a few times already. I can not think of anything that goes wrong and I am able to reproduce this error on a new jupyter notebook as well.

import pandas as pd

columns = {'Start Time', 'Open', 'High', 'Low', 'Close', 'Volume',
          'End Time', 'Amount', 'No. Trades', 'Taker Buy Base',
          'Taker Buy Quote', 'N'}
df = pd.DataFrame(test_data, columns=columns)
df

Sample data

test_data = [[1617231600000,
  '538.32000000',
  '545.15000000',
  '535.05000000',
  '541.06000000',
  '8663.58299000',
  1617235199999,
  '4686031.35064850',
  11361,
  '5051.86680000',
  '2733476.69840350',
  '0'],
 [1617235200000,
  '541.11000000',
  '554.67000000',
  '541.00000000',
  '552.49000000',
  '13507.97221000',
  1617238799999,
  '7404389.49931720',
  14801,
  '7736.80002000',
  '4242791.14275430',
  '0'],
 [1617238800000,
  '552.58000000',
  '553.73000000',
  '544.82000000',
  '548.50000000',
  '7115.15238000',
  1617242399999,
  '3907155.60293150',
  5556,
  '3580.46860000',
  '1964701.27448790',
  '0'],
 [1617242400000,
  '548.49000000',
  '550.63000000',
  '544.70000000',
  '545.45000000',
  '3589.18173000',
  1617245999999,
  '1964514.51702120',
  3974,
  '1742.76278000',
  '954042.85262340',
  '0'],
 [1617246000000,
  '545.80000000',
  '545.80000000',
  '540.48000000',
  '541.56000000',
  '4767.67233000',
  1617249599999,
  '2586841.14566500',
  4960,
  '2516.25626000',
  '1364734.14900580',
  '0']]

Expected output: The column names should be labelled in the same order as described in the columns variable. It is labelled like this for me, clearly the wrong order. There is another post but it did not help.

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
Mr. rk
  • 127
  • 1
  • 5

1 Answers1

2

Python sets do not maintain order. Use a list (square brackets instead of curly) instead.

columns = ['Start Time', 'Open', 'High', 'Low', 'Close', 'Volume',
          'End Time', 'Amount', 'No. Trades', 'Taker Buy Base',
          'Taker Buy Quote', 'N']
tdelaney
  • 55,698
  • 4
  • 59
  • 89