赞
踩
import pandas as pdimport numpy as np# 创建空白DataFramedf = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
for i in range(4): df.loc[i] = [np.random.randint(-1, 1) for n in range(3)]df.loc[i] = 5 #添加一条数据都为5的记录
df.append({'lib': 2, 'qty1': 3, 'qty2': 4}, ignore_index=True) # append也可以直接添加DataFramedf2 = pd.DataFrame([[1,2,3], [2,3,4]], columns=['lib', 'qty1', 'qty2'])df.append(df2, ignore_index=True) # ignore_index设置为True,index将会忽略df2的index
循环将要添加的数据以字典的形式保存到一个列表中,在用列表创建出DataFrame
row_list = [] input_rows = [[1,2,3], [2,3,4]] # 待插入数据for row in input_rows: dict1 = dict(lib=row[0], qty1=row[1], qty2=row[2]) # 将数据转为字典 row_list.append(dict1) # 保存到列表中df = pd.DataFrame(row_list)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。