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
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值