1.添加一行的方法
...: data = {
...: 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
...: 'year': [2000, 2001, 2002, 2001, 2002, 2003],
...: 'pop': [2.5, 1.7, 3.6, 2.4, 2.9, 3.2],
...: }
...:
...: frame = pd.DataFrame(data)
In [13]: frame.iloc[6] = [1,'asf',201] #这一行会报错IndexError: single positional indexer is out-of-bounds、
In [14]: frame.loc[6]=[1,'hh',612] #正确添加一行的方法
In [17]: frame.iloc[7,:]=[1,'hh',612] #正确添加一行的方法
In [18]: frame.ix[7,:]=[1,'hh',612] #正确添加一行的方法
2.#想添加一行只有数组组成
In [24]: frame['show'] = [5,0,4] #报错 ValueError: Length of values does not match length of index
#原因是,列表的长度应该和索引一致
frame['show'] = [[0,0,0] for i in range(len(frame))]
3.某一列的元素为列表,修改其中的值
frame.loc[2,'show']=[1,2,5] #报错键和值不符
暂时没有找到方法。使用的方法: frame.loc[2,'show'][0] = 1 .......
4.去掉重复的行
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
subset : column label or sequence of labels, optional
用来指定特定的列,默认所有列
keep : {‘first’, ‘last’, False}, default ‘first’
删除重复项并保留第一次出现的项
inplace : boolean, default False
是直接在原来数据上修改还是保留一个副本
5.pandas dataframe清空表格的方法
df.drop(df.index,inplace=True)