pandas.DataFrame.append

DataFrame.append(other, ignore_index=False, verify_integrity=False)
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html

DataFrame连接字典

In [1]: import pandas as pd
   ...: df=pd.DataFrame({
   ...:     "name":[],
   ...:     "age":[]
   ...:         })
   ...: student={
   ...:         "name":"wang",
   ...:         "age":1
   ...:         }
   ...: df.append(pd.DataFrame(student))
ValueError: If using all scalar values, you must pass an index

提示这样的错误是因为字典在转为DataFrame的时候没有给出索引,应该在转换的时候给出索引

给字典转换的数据框添加索引

In [1]: import pandas as pd
   ...: df=pd.DataFrame({
   ...:     "name":[],
   ...:     "age":[]
   ...:         })
   ...: student={
   ...:         "name":"wang",
   ...:         "age":1
   ...:         }
   ...: df.append(pd.DataFrame(student,index=[1]))#注意这里index传入的是列表
Out[1]: 
   age  name
1  1.0  wang

这届添加字典,忽略掉索引

In [1]: import pandas as pd
   ...: df=pd.DataFrame({
   ...:     "name":[],
   ...:     "age":[]
   ...:         })
   ...: student={
   ...:         "name":"wang",
   ...:         "age":1
   ...:         }
   ...: df.append(student,ignore_index=True)
Out[1]: 
   age  name
0  1.0  wang

你可能感兴趣的:(pandas)