Python Pandas. Dataframe adjusting column order and modifying the index name

Source: Internet
Author: User

1. Create a dataframe from a dictionary

>>>ImportPandas>>> dict_a = {'user_id':['Webbang','Webbang','Webbang'],'book_id':['3713327','4074636','26873486'],'rating':['4','4','4'],'mark_date':['2017-03-07','2017-03-07','2017-03-07']}>>> df = Pandas. DataFrame (DICT_A)#Create a dataframe from a dictionary>>> DF#The created DF column names are sorted alphabetically by default, and the order in the dictionary is not the same, the dictionary is ' user_id ', ' book_id ', ' rating ', ' mark_date 'book_id mark_date Rating user_id03713327 2017-03-07 4Webbang1 4074636 2017-03-07 4Webbang2 26873486 2017-03-07 4 Webbang

2. Adjust column order

>>> df = df[['user_id','book_id','  Rating','mark_date'#  Adjust the column order to ' user_id ', ' book_id ', ' Rating ', ' mark_date '>>> df   user_id   book_id rating   mark_date0  Webbang    3713327      4  2017-03-071  webbang   4074636      4  2017-03-072  Webbang  26873486      4  2017-03-07

3. Adjust index to start from 1

# change Index to start from 1 >>> df   user_id   book_id rating   mark_date1  webbang   3713327      4  2017-03-072  webbang   4074636      4  2017-03-073  Webbang  26873486      4  2017-03-07

Dataframe Operation Summary: http://www.cnblogs.com/huahuayu/p/8227494.html

Python Pandas. Dataframe adjusting column order and modifying the index name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.