所有数据请参考:
https://github.com/datawhalechina/team-learning/tree/master/Pandas%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%8A%EF%BC%89
import numpy as np
import pandas as pd
df = pd.read_csv('data/table.csv')
df.head()
一、append与assign
1. append方法
(a)利用序列添加行(必须指定name)
df_append = df.loc[:3,['Gender','Height']].copy()
df_append
s = pd.Series({
'Gender':'F','Height':188},name='new_row')
df_append.append(s)
(b)用DataFrame添加表
df_temp = pd.DataFrame({
'Gender':['F','M'],'Height':[188,176]},index=['new_1','new_2'])
df_append.append(df_temp)
2. assign方法
该方法主要用于添加列,列名直接由参数指定:
s = pd.Series(list('abcd'),index=range(4))
df_append.assign(Letter=s)
可以一次添加多个列:
df_append