Pandas Basics-Level 2: Understand the data processing object-DataFrame

mission details

Tasks for this level: According to the programming requirements, complete the writing of relevant codes.

related information

DataFrameIt is a tabular data structure, which is a data table (hierarchical index) stored in one or more two-dimensional blocks. It DataFramehas both row index and column index. It has a set of ordered columns. Each column can be different Data of type (numeric, string, boolean), or can be viewed as Seriesa dictionary composed of. DataFrame creation:

 
 
  1. dictionary = {'state':['0hio','0hio','0hio','Nevada','Nevada'],
  2. 'year':[2000,2001,2002,2001,2002],
  3. 'pop':[1.5,1.7,3.6,2.4,2.9]}
  4. frame = DataFrame(dictionary)

Modify row name:

 
 
  1. frame=DataFrame(dictionary,index=['one','two','three','four','five'])

Add modifications:

 
 
  1. frame['add']=[0,0,0,0,0]

Add Series type:

 
 
  1. value = Series([1,3,1,4,6,8],index = [0,1,2,3,4,5])
  2. frame['add1'] = value
Programming requirements

Follow the prompts and add code in the editor on the right begin-end:

  • Create an array named with five rows and three columns, with column names df1and row names ;DataFrame[states,years,pops]['one','two','three','four','five']

  • Add df1a new column with a column name new_addand a value of [7,4,5,8,2].

Test instruction

If the answer is correct, it will be output True.

# -*- coding: utf-8 -*-
from pandas import Series,DataFrame
import  pandas as pd

def create_dataframe():
    '''
    返回值:
    df1: 一个DataFrame类型数据
    '''
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
    df1 = pd.DataFrame(columns = ['states','years','pops'],index = ['one','two','three','four','five'])
    df1['new_add'] = [7,4,5,8,2]
    # ********** End **********#

    #返回df1
    return df1

Guess you like

Origin blog.csdn.net/Joy19981127/article/details/134758609