1

I'm trying to make a graph of pay against time. Here's what I've got:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('Pay051118_1.csv')
df.to_csv("Newcsv", columns = ['Dates worked', 'Pay'])
df = pd.read_csv('Newcsv', parse_dates = True, index_col = 0)

df = df.fillna(0)

df = ((df[['Dates worked','Pay']])[df.Pay > 1])
print (df)

df.plot()
plt.show()

Line 5 of the code got rid of one of the indexing columns for me. Without putting in the parse_dates and index_col parameter, I will get 2 columns of indexes. When I print (df), this is what I get:

                   Dates worked   Pay
0       Monday, 5 November 2018  44.5
2    Wednesday, 7 November 2018  44.5
3     Thursday, 8 November 2018  44.5
4       Friday, 9 November 2018  44.5
6      Sunday, 11 November 2018  50.5
7      Monday, 12 November 2018  62.0
8     Tuesday, 13 November 2018  38.5
9   Wednesday, 14 November 2018  65.5
10   Thursday, 15 November 2018  62.0
16  Wednesday, 21 November 2018  65.5
17   Thursday, 22 November 2018  62.0
18     Friday, 23 November 2018  65.5
20     Sunday, 25 November 2018  62.5
21     Monday, 26 November 2018  58.5
22    Tuesday, 27 November 2018  42.0
23  Wednesday, 28 November 2018  65.5
25     Friday, 30 November 2018  62.0

As you can see, the indexing column is still there. How can I get rid of it?

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
koifish
  • 424
  • 1
  • 6
  • 16
  • Data frames always have an index, what you can do is to reset it to something else, for example the dates. This can be achieved by `df.set_index`. – a_guest Dec 04 '18 at 01:19
  • Hi, you don't need to import, export, and re-import your CSV. You can select columns on import. See pandas.pydata.org/pandas-docs/stable/io.html#io-read-csv-table. After that, see https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html. We don't have access to your csv. – Evan Dec 04 '18 at 01:25
  • Possible duplicate of [Could pandas use column as index?](https://stackoverflow.com/questions/38542419/could-pandas-use-column-as-index) – Evan Dec 04 '18 at 01:28

3 Answers3

1

Use below one.This will print without left most row of numbers which you dont want to see.

print df.to_string(index=False)
LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49
  • Thanks, it worked. But by I way why must we just the df.to_string code? What does converting it to string do? – koifish Dec 04 '18 at 01:35
  • It Render a DataFrame to a console-friendly tabular output. You can refer below link. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_string.html – LOrD_ARaGOrN Dec 04 '18 at 01:39
0

Pandas dataframes always have an index but it is not real column, If you want to replace the index with simple sequential numbers, use df.reset_index().

Aditya Lahiri
  • 409
  • 3
  • 11
-1
"""
Dates worked,Pay
Monday 5 November 2018,44.5
Wednesday 7 November 2018,44.5
Thursday 8 November 2018,44.5
Friday 9 November 2018,44.5
"""
import pandas as pd
pd.read_clipboard(sep=",").set_index("Dates worked")

If you don't want an auto-generated index, you'll need to set some other column as the index.

Evan
  • 2,121
  • 14
  • 27