pandas DataFram的insert函数

原文链接:https://blog.csdn.net/yanwucao/article/details/80211984

 

 

DataFrame.insert(loc, column, value, allow_duplicates=False)

 

Insert column into DataFrame at specified location.

 

Raises a ValueError if column is already contained in the DataFrame,unless allow_duplicates is set to True.

 

在指定的地方插入一列数据。如果dataframe中已经存在某列,将allow_duplicates置为true才可以将指定得列插入。

 

Parameters:
loc : int

 

Insertion index. Must verify 0 <= loc <= len(columns)  要插入的那一列

 

column : string, number, or hashable object

 

label of the inserted column    要插入那列的标签

 

value : int, Series, or array-like

 

allow_duplicates : bool, optional    布尔类型,可选择

 

 

 

举实例如下:

 

import numpy as np
import pandas as pd

# A = np.array([[1, 1], [1, 2], [1, 3]])
# print(np.shape(A))

df = pd.DataFrame(np.random.randint(1, 10, (6, 4)), index=None, columns=list('ABCD'))
df['E'] = pd.Series([1, 1, 1, 1, 1, 1])
s = pd.Series([1, 1, 1, 1, 1, 1])
df.insert(1, 'X', s)
print(df)
运行结果:

 

   A  X  B  C  D  E
0  7  1  6  6  1  1
1  6  1  7  3  8  1
2  8  1  3  7  1  1
3  3  1  5  4  9  1
4  6  1  9  6  5  1
5  6  1  5  6  5  1
---------------------
作者:yanwucao
来源:CSDN
原文:https://blog.csdn.net/yanwucao/article/details/80211984
版权声明:本文为博主原创文章,转载请附上博文链接!

 

转载于:https://www.cnblogs.com/loubin/p/11296418.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值