DataFrame新增行或列(三)

1.在DataFrame上增加列

1.1.使用元组或列表增加列

df = pd.DataFrame([10,20,30,40], columns=['numbers'])
print(df)
print('---------------------------')
df['float'] = (1.2,2.2,3.2,4.2) # 使用元组或列表增加列
print(df)

输出结果:
---------------------------
   numbers
0       10
1       20
2       30
3       40
---------------------------
   numbers  float
0       10    1.2
1       20    2.2
2       30    3.2
3       40    4.2

1.2.使用DataFrame创建一个新列

# 整个DataFrame对象定义一个新列
df['names'] = pd.DataFrame(['Yves', 'Guido', 'Felix', 'Francesc']) 
print(df)

输出结果:
   numbers  float     names
0       10    1.2      Yves
1       20    2.2     Guido
2       30    3.2     Felix
3       40    4.2  Francesc

2.使用append增加行

DataFrame.append(*other*, *ignore_index=False*, *verify_integrity=False*, *sort=None*)

other: 是要添加的数据,append很不挑食,这个other可以是dataframe,dict,Seris,list等等。
ignore_index: 参数为True时将在数据合并后,按照0,1,2,3....的顺序重新设置索引,忽略了旧索引。
verify_integrity:参数为True时,如果合并的数据与原数据包含索引相同的行,将报错。

    append使用字典添加新行

# 使用字典
df2 = df.append({'numbers':100, 'float':5.75, 'names':'Henry'},ignore_index=True)
print(df2)

输出结果:
   numbers  float     names
0       10   1.20      Yves
1       20   2.20     Guido
2       30   3.20     Felix
3       40   4.20  Francesc
4      100   5.75     Henry

    append使用DataFrame添加新行:

#使用DataFrame添加新行
df2 = df.append(pd.DataFrame({'numbers':100, 'float':5.75, 'names':'Henry'}, index=['z']))
print(df2)

输出结果:
   numbers  float     names
0       10   1.20      Yves
1       20   2.20     Guido
2       30   3.20     Felix
3       40   4.20  Francesc
z      100   5.75     Henry

3.使用join增加列

通过索引或者指定的列连接两个DataFrame。
DataFrame.join(other, on=None, how=’left’, lsuffix=”, rsuffix=”, sort=False)

参数说明
other:【DataFrame,或者带有名字的Series,或者DataFrame的list】如果传递的是Series,那么其name属性应当是一
个集合,并且该集合将会作为结果DataFrame的列名
on:【列名称,或者列名称的list/tuple,或者类似形状的数组】连接的列,
默认使用索引连接
how:【{‘left’, ‘right’, ‘outer’, ‘inner’}, default:‘left’】连接的方式,默认为左连接
lsuffix:【string】左DataFrame中重复列的后缀
rsuffix:【string】右DataFrame中重复列的后缀
sort:【boolean, default False】按照字典顺序对结果在连接键上排序。如果为False,连接键的顺序取决于连接类型(关键字)。

df1 = pd.DataFrame({'a':[1,2],'b':[3,4]})
df2 = pd.DataFrame({'c':[5,6,7],'d':[8,9,0]})
df = df1.join(df2)   # 默认how=left,左连接
print(df)
print('--------------------')
df = df1.join(df2, how='outer') # how=outer,外连接
print(df)

输出结果:
---------------------------
   a  b  c  d
0  1  3  5  8
1  2  4  6  9
--------------------
     a    b  c  d
0  1.0  3.0  5  8
1  2.0  4.0  6  9
2  NaN  NaN  7  0

4.使用concat增加行列

pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True)
常用参数:
obj:要合并的series,dataframe或者是panel构成的序列,常将这些数据排成一个列表[data1,data2....]。
axis:按照哪个方向拼接,0是纵向拼接(默认),1是横向拼接。
join:设置合并取交集(inner)还是并集(outer)。纵向拼接时取column的交并集,横向拼接时取index的交并集。
join_axes:index的列表,仅在横向合并时使用,指明要将数据合并入哪个原表的index。
ignore_index:如果设置为true,则无视表的index,直接合并,合并后生成新的index。
keys:表标识的列表,用来区分合并的表来自哪里。

df1 = pd.DataFrame([[1,2],[3,4]])
df2 = pd.DataFrame([[5,6],[7,8]])
df = pd.concat([df1,df2], axis=0)  # axis=0,纵向合并,增加行
print(df)
print('-----------------------')
df = pd.concat([df1,df2], axis=1)  # axis=1,横向合并,增加列
print(df)


输出结果:
   0  1
0  1  2
1  3  4
0  5  6
1  7  8
-----------------------
   0  1  0  1
0  1  2  5  6
1  3  4  7  8

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值