pyhton3 pandas dataframe数据的增删改查

def test10():
    INPUTFILE1 = os.path.join(BASE_PATH, 'Students6.xlsx')
    datas = ExcelUntil.readExcelFile(INPUTFILE1, headerRow=0)
    page_01=datas['Page_001']
    page_02=datas['Page_002']
    # 追加已有
    students=page_01.append(page_02).reset_index(drop=True)
    print(students)
    # 追加新建
    stu=pd.Series({'ID':43,'Name':'Abel','Score':98})
    students=students.append(stu,ignore_index=True)
    print(students)
    # 删除
    students=students.drop(index=[38,39])
    print(students)
    # 插入
    stu=pd.Series({'ID':100,'Name':'Bail','Score':82})
    part1=students[:21]
    part2=students[21:]
    students=part1.append(stu,ignore_index=True).append(part2,ignore_index=True)
    print(students)
    # 更改
    stu=pd.Series({'ID':101,'Name':'lina','Score':100})
    students.iloc[38]=stu
    print(students)
    students.loc[37,'Name']='Lili'
    print(students)
    # 设置空值
    for i in range(5,15):
        students['Name'].at[i]=' '
    # 去除空值
    missing=students.loc[students['Name']==' ']
    students.drop(missing.index,inplace=True)
    print(students)
    students=students.reset_index(drop=True)
    print(students)
def test11():
    INPUTFILE1 = os.path.join(BASE_PATH, 'Students6.xlsx')
    datas = ExcelUntil.readExcelFile(INPUTFILE1, headerRow=0)
    page_01=datas['Page_001']
    page_02=datas['Page_002']
    # 追加列
    page_01['Age']=26
    print(page_01)
    # 删除列
    page_01.drop(columns=['Score','Age'],inplace=True)
    print(page_01)
    #插入列
    page_01.insert(1,column='Foo',value=np.repeat('for',len(page_01)))
    print(page_01)
    #改列名
    page_01.rename(columns={'Foo':'FOO','Name':'NAME'},inplace=True)
    print(page_01)
    # 设置空值
    page_01['ID']=page_01['ID'].astype(float)
    for i in range(5,10):
        page_01['ID'].at[i]=np.nan
        page_01.loc[i,'NAME']='小米'
    print(page_01)
    # 去掉空值
    page_01.dropna(inplace=True)
    print(page_01)
    page_01=page_01.reset_index(drop=True)
    print(page_01)

 

你可能感兴趣的:(Python)