这篇文章主要介绍python在数组中添加一行数据的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
python在数组中添加一行数据的方法:
1、直接插入一行:
import pandas as pd
from pandas import DataFrame
df3=DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])
# 新插入的行一定要加 index,不然会报错
df3.loc['new'] = ['a','a','a','a']
# 运行结果为
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
new a a a a
2、新建一个同样的 dataframe, 然后合并两个dataframe
df4 = pd.DataFrame([6,6,6,6]).T
# 修改df4的column和df3的一致
df4.columns = df3.columns
# 把两个dataframe合并,需要设置 ignore_index=True
df_new = pd.concat([df3,df4],ignore_index=True)
# 运行结果
one two three four
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
4 a a a a
5 6 6 6 6
以上是python在数组中添加一行数据的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。