मेरे पास निम्न कोड है और मैं इंडेक्स को छोड़ने और सेकंड कॉलम का नाम बदलने में असमर्थ हूं। इंडेक्स और दूसरे कॉलम दोनों का एक ही नाम "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
4
jason 4 अप्रैल 2018, 17:16

3 जवाब

सबसे बढ़िया उत्तर

बदलाव के लिए rename_axis का इस्तेमाल करें अनुक्रमणिका नाम, यदि index से कॉलम की आवश्यकता है तो reset_index और यदि आवश्यक हो तो rename का उपयोग करें:

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
4
jezrael 4 अप्रैल 2018, 14:23

वहां क्या है उसे अधिलेखित करने के लिए अनुक्रमणिका सेट करें।

df.set_index('Date')

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

अपना नामकरण पूरा करने के लिए

df.set_index('Date').rename_axis('SomeName').reset_index()

      SomeName   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
2
piRSquared 4 अप्रैल 2018, 14:18

रीसेट इंडेक्स का उपयोग करने का प्रयास करें:

df.reset_index(drop = True)
1
Brian 4 अप्रैल 2018, 14:18