0

I am new to pandas and was checking out Pandas.Dataframe. I realize that always I am creating a dataframe, a first column with no column title is created which is also the index column. For example the following code:

from collections import OrderedDict
table = OrderedDict((
    ("Item", ['Item0', 'Item0', 'Item1', 'Item1']),
    ('CType',['Gold', 'Bronze', 'Gold', 'Silver']),
    ('USD',  ['1$', '2$', '3$', '4$']),
    ('EU',   ['1€', '2€', '3€', '4€'])
))
df = pd.DataFrame(table)

Gives the result:

    Item    CType   USD     EU
0   Item0   Gold    1$  1€
1   Item0   Bronze  2$  2€
2   Item1   Gold    3$  3€
3   Item1   Silver  4$  4€

But the first column is not listed in the columns. Because if I execute len(df.columns) I will get 4. Or print(list(df)) will list: ['Item', 'CType', 'USD', 'EU']. But if I execute df.index.values I will get [0 1 2 3]. So, what exactly is the first column and can it be avoided?

Code Pope
  • 5,075
  • 8
  • 26
  • 68
  • First column should be avoided if write to file by `df = pd.to_csv(file, index=False)` – jezrael Aug 20 '18 at 12:34
  • But by design each DataFrame have `index` – jezrael Aug 20 '18 at 12:34
  • According to the [`pandas==0.23.4` documentation](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.index.html): `DataFrame.index` _The index (row labels) of the DataFrame._ – tafaust Aug 20 '18 at 12:37
  • 1
    As everyone mentioned it is just an index. It is really no different than the row index in excel. If you wish to set a column as the index you can use `df.set_index('column_to_index')` – It_is_Chris Aug 20 '18 at 12:49
  • And it is not possible to create a dataframe without index? – Code Pope Aug 20 '18 at 12:57
  • @CodePope No, you cannot have a dataframe without an index. It is necessary. Look at the duplicate answer to see why. – It_is_Chris Aug 20 '18 at 13:17

0 Answers0