A method of selecting rows and columns based on PANDAS data samples

Source: Internet
Author: User
Below for you to share a pandas data samples based on the selection of methods, has a good reference value, I hope to be helpful to everyone. Come and see it together.

Note: The following code is written based on the python3.5.0

Import Pandasfood_info = Pandas.read_csv ("Food_info.csv") #------------------Select the first row of the data sample--------------------print ( FOOD_INFO.LOC[0] #------------------Select 3 to 6 rows of data Samples----------------------print (Food_info.loc[3:6]) #--------------- ---Head selects the first few lines of the data sample------------------print (Food_info.head (2)) #------------------Select the 2,5,10 row of the data sample,-----------the two methods # Print (food_info.loc[[2,5,10]]) #方法一 two_five_ten = [2,5,10] #方法二print (Food_info.loc[two_five_ten]) #---------- --------Select the Ndb_no column of the Data sample--------------------# Ndb_col = food_info["Ndb_no"] #方法一 col_name = "Ndb_no" #方法二ndb_c OL = Food_info[col_name]print (ndb_col) #------------------Select multiple columns of data samples-------------------# zinc_copper = food_info[[" Zinc_ (Mg) "," Copper_ (mg) "]]columns = [" Zinc_ (Mg) "," Copper_ (mg) "]zinc_copper = Food_info[columns]print (zinc_copper) # ---------------------Comprehensive Small example----------------------------col_names = Food_info.columns.tolist () #把所有的行转化成listprint ( col_names) Gram_columns = []for C in Col_names: #遍历col_names, find out all theg) End Position if C.endswith ("(g)"): Gram_columns.append (c) print (gram_columns) GRAM_DF = Food_info[gram_columns] #把所有以 (g) knot The column of the tail is deposited to Gram_dfprint (Gram_df.head (3))

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.