gpt4 book ai didi

python - 无法删除索引列并重命名第二列

转载 作者:行者123 更新时间:2023-12-02 05:11:42 25 4
gpt4 key购买 nike

我有以下代码,但无法删除索引并重命名秒列。索引和第二列具有相同的名称“Date”。我该如何修复它?

              Date     Open     High      Low     Last    Close  \
Date
2010-12-31 2010-12-31 71.50 74.00 71.50 72.05 72.40
2011-01-31 2011-01-31 70.05 73.00 70.05 72.70 72.75
2011-02-28 2011-02-28 77.00 77.00 74.80 74.90 74.85
2011-03-31 2011-03-31 54.35 58.85 54.35 55.00 55.25
2011-04-30 2011-04-29 60.50 60.50 58.10 58.10 58.20
2011-05-31 2011-05-31 52.50 53.50 52.10 52.95 52.55
2011-06-30 2011-06-30 46.30 46.95 45.60 46.60 46.65
2011-07-31 2011-07-29 49.70 50.40 49.30 49.30 49.60
2011-08-31 2011-08-30 45.10 47.90 43.05 47.85 47.45
2011-09-30 2011-09-30 42.05 43.45 41.40 41.75 41.85
2011-10-31 2011-10-31 43.70 43.70 41.10 43.70 43.70
2011-11-30 2011-11-30 39.95 39.95 38.05 38.15 38.15
2011-12-31 2011-12-30 31.55 35.80 31.15 34.00 34.10
2012-01-31 2012-01-31 44.95 45.30 43.70 44.00 44.00
2012-02-29 2012-02-29 47.90 49.00 46.15 47.00 46.25
2012-03-31 2012-03-30 44.75 44.75 43.60 44.40 44.45
2012-04-30 2012-04-30 70.00 75.60 66.70 75.60 75.60

最佳答案

使用rename_axis要更改索引名称,如果需要 index 中的列,请添加 reset_index如有必要,请使用重命名:

df = df.rename_axis('Date1').reset_index().rename(columns={'Date':'New'})
print (df)
Date1 New Open High Low Last Close
0 2010-12-31 2010-12-31 71.50 74.00 71.50 72.05 72.40
1 2011-01-31 2011-01-31 70.05 73.00 70.05 72.70 72.75
2 2011-02-28 2011-02-28 77.00 77.00 74.80 74.90 74.85
3 2011-03-31 2011-03-31 54.35 58.85 54.35 55.00 55.25
4 2011-04-30 2011-04-29 60.50 60.50 58.10 58.10 58.20
5 2011-05-31 2011-05-31 52.50 53.50 52.10 52.95 52.55
6 2011-06-30 2011-06-30 46.30 46.95 45.60 46.60 46.65
7 2011-07-31 2011-07-29 49.70 50.40 49.30 49.30 49.60
8 2011-08-31 2011-08-30 45.10 47.90 43.05 47.85 47.45
9 2011-09-30 2011-09-30 42.05 43.45 41.40 41.75 41.85
10 2011-10-31 2011-10-31 43.70 43.70 41.10 43.70 43.70
11 2011-11-30 2011-11-30 39.95 39.95 38.05 38.15 38.15
12 2011-12-31 2011-12-30 31.55 35.80 31.15 34.00 34.10
13 2012-01-31 2012-01-31 44.95 45.30 43.70 44.00 44.00
14 2012-02-29 2012-02-29 47.90 49.00 46.15 47.00 46.25
15 2012-03-31 2012-03-30 44.75 44.75 43.60 44.40 44.45
16 2012-04-30 2012-04-30 70.00 75.60 66.70 75.60 75.60

或者如果需要删除索引并重命名:

df = df.reset_index(drop=True).rename(columns={'Date':'New'})
print (df)
New Open High Low Last Close
0 2010-12-31 71.50 74.00 71.50 72.05 72.40
1 2011-01-31 70.05 73.00 70.05 72.70 72.75
2 2011-02-28 77.00 77.00 74.80 74.90 74.85
3 2011-03-31 54.35 58.85 54.35 55.00 55.25
4 2011-04-29 60.50 60.50 58.10 58.10 58.20
5 2011-05-31 52.50 53.50 52.10 52.95 52.55
6 2011-06-30 46.30 46.95 45.60 46.60 46.65
7 2011-07-29 49.70 50.40 49.30 49.30 49.60
8 2011-08-30 45.10 47.90 43.05 47.85 47.45
9 2011-09-30 42.05 43.45 41.40 41.75 41.85
10 2011-10-31 43.70 43.70 41.10 43.70 43.70
11 2011-11-30 39.95 39.95 38.05 38.15 38.15
12 2011-12-30 31.55 35.80 31.15 34.00 34.10
13 2012-01-31 44.95 45.30 43.70 44.00 44.00
14 2012-02-29 47.90 49.00 46.15 47.00 46.25
15 2012-03-30 44.75 44.75 43.60 44.40 44.45
16 2012-04-30 70.00 75.60 66.70 75.60 75.60

如果只需要重命名索引名称和列名称:

df = df.rename_axis('Date1').rename(columns={'Date':'New'})
print (df)
New Open High Low Last Close
Date1
2010-12-31 2010-12-31 71.50 74.00 71.50 72.05 72.40
2011-01-31 2011-01-31 70.05 73.00 70.05 72.70 72.75
2011-02-28 2011-02-28 77.00 77.00 74.80 74.90 74.85
2011-03-31 2011-03-31 54.35 58.85 54.35 55.00 55.25
2011-04-30 2011-04-29 60.50 60.50 58.10 58.10 58.20
2011-05-31 2011-05-31 52.50 53.50 52.10 52.95 52.55
2011-06-30 2011-06-30 46.30 46.95 45.60 46.60 46.65
2011-07-31 2011-07-29 49.70 50.40 49.30 49.30 49.60
2011-08-31 2011-08-30 45.10 47.90 43.05 47.85 47.45
2011-09-30 2011-09-30 42.05 43.45 41.40 41.75 41.85
2011-10-31 2011-10-31 43.70 43.70 41.10 43.70 43.70
2011-11-30 2011-11-30 39.95 39.95 38.05 38.15 38.15
2011-12-31 2011-12-30 31.55 35.80 31.15 34.00 34.10
2012-01-31 2012-01-31 44.95 45.30 43.70 44.00 44.00
2012-02-29 2012-02-29 47.90 49.00 46.15 47.00 46.25
2012-03-31 2012-03-30 44.75 44.75 43.60 44.40 44.45
2012-04-30 2012-04-30 70.00 75.60 66.70 75.60 75.60

关于python - 无法删除索引列并重命名第二列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com