如何摆脱Pandas中的第二个索引列? (蟒蛇)

时间:2018-12-04 01:10:51

标签: python pandas

我正在尝试绘制薪水与时间的关系图。这就是我所拥有的:

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()

代码的第5行为我摆脱了其中一个索引列。不输入parse_dates和index_col参数,我将获得2列索引。当我打印(df)时,这就是我得到的:

                   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

如您所见,索引列仍然存在。我该如何摆脱呢?

3 个答案:

答案 0 :(得分:1)

在1以下使用。这会打印出最不想看到的最左边的数字。

print df.to_string(index=False)

答案 1 :(得分:0)

Pandas数据帧始终具有索引,但不是实列,如果要用简单的序列号替换索引,请使用df.reset_index()

答案 2 :(得分:-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")

如果您不希望自动生成索引,则需要将其他一些列设置为索引。