網(wǎng)站首頁 編程語言 正文
向二維數(shù)組中添加整行或者增列元素
1、numpy中empty方法可以創(chuàng)建一個二維數(shù)組
x=np.empty(shape=[0,4],dtype=int)
創(chuàng)建了一個空的四列的二維數(shù)組
2、添加需要的元素
x=np.append(x,[[1,2,3,4]],axis=0)#添加整行元素,axis=1添加整列元素
如何在數(shù)組中添加一行數(shù)據(jù)
python在數(shù)組中添加一行數(shù)據(jù)的方法:
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']
??
# 運(yùn)行結(jié)果為??
? ? 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合并,需要設(shè)置 ignore_index=True
df_new = pd.concat([df3,df4],ignore_index=True)
??
# 運(yùn)行結(jié)果??
? ? 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
總結(jié)
原文鏈接:https://blog.csdn.net/ShaoDu/article/details/94630657
相關(guān)推薦
- 2022-04-03 Nginx構(gòu)建Tomcat集群的操作方法_nginx
- 2022-08-04 Python?threading和Thread模塊及線程的實(shí)現(xiàn)_python
- 2022-10-23 C#在Entity?Framework中實(shí)現(xiàn)事務(wù)回滾_C#教程
- 2021-12-02 C語言GetStdHandle函數(shù)使用方法_C 語言
- 2023-02-05 python??fire庫的使用實(shí)例教程_python
- 2022-05-28 C++實(shí)現(xiàn)簡單的學(xué)生成績管理系統(tǒng)_C 語言
- 2022-08-19 React中Props的使用
- 2022-12-12 Go語言開發(fā)前后端不分離項目詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支