pandas增加列的七种方法

insert

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                             'B': ['B0', 'B1', 'B2']},
                            index=[1.0, 2.0, 3.0])

    df.insert(0, 'A1', ['A00', 'A01', 'A02'])
    print(df)
python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A0   A   B
1.0  A00  A0  B0
2.0  A01  A1  B1
3.0  A02  A2  B2

赋值

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                             'B': ['B0', 'B1', 'B2']},
                            index=[1.0, 2.0, 3.0])

    print(df)
    df["C"] = ['C0', 'C1', 'C2']
    print(df)

python 复制代码
df['C'] = df['A'].str.replace("A", "C")
df['C'] = df['A'].map(lambda x: x.replace('A', 'C'))
python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B   C
1.0  A0  B0  C0
2.0  A1  B1  C1
3.0  A2  B2  C2

loc

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                             'B': ['B0', 'B1', 'B2']},
                            index=[1.0, 2.0, 3.0])

    print(df)
    df.loc[:, "C"] = ['C0', 'C1', 'C2']
    print(df)
python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B   C
1.0  A0  B0  C0
2.0  A1  B1  C1
3.0  A2  B2  C2

类似上面的。

concat

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                           'B': ['B0', 'B1', 'B2']},
                          index=[1.0, 2.0, 3.0])
    df1 = pandas.Series(['C0', 'C1', 'C2'], index=[1.0, 2.0, 3.0])

    print(df)
    df = pandas.concat([df, df1], axis=1)
    print(df)

apply、map

map

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                           'B': ['B0', 'B1', 'B2']},
                          index=[1.0, 2.0, 3.0])
    print(df)
    df['C'] = df['A'].map(lambda x: x.replace('A', 'C'))
    print(df)

结果

python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B   C
1.0  A0  B0  C0
2.0  A1  B1  C1
3.0  A2  B2  C2

apply

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                           'B': ['B0', 'B1', 'B2']},
                          index=[1.0, 2.0, 3.0])
    print(df)
    df['C'] = df.apply(lambda x, s1, s2: x[s1]+x[s2], args=('A', 'B'), axis=1)
    print(df)
python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B     C
1.0  A0  B0  A0B0
2.0  A1  B1  A1B1
3.0  A2  B2  A2B2

reindex

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                           'B': ['B0', 'B1', 'B2']},
                          index=[1.0, 2.0, 3.0])

    print(df)
    df = df.reindex(columns=df.columns.tolist()+['C'], fill_value=1)
    print(df)
python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B  C
1.0  A0  B0  1
2.0  A1  B1  1
3.0  A2  B2  1

assign

python 复制代码
def test1():
    df = pandas.DataFrame({'A': ['A0', 'A1', 'A2'],
                           'B': ['B0', 'B1', 'B2']},
                          index=[1.0, 2.0, 3.0])
    print(df)
    df = df.assign(C=df["A"]+df['B'], D=df["B"]+df['A'])
    print(df)

结果:

python 复制代码
      A   B
1.0  A0  B0
2.0  A1  B1
3.0  A2  B2
      A   B     C     D
1.0  A0  B0  A0B0  B0A0
2.0  A1  B1  A1B1  B1A1
3.0  A2  B2  A2B2  B2A2

参考

https://blog.csdn.net/lzjhyhf/article/details/129205949

相关推荐
猫头虎4 小时前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
peter676810 小时前
pandas学习小结
学习·pandas
猫头虎19 小时前
如何解决 pip install -r requirements.txt extras 语法 ‘package[extra’ 缺少 ‘]’ 解析失败问题
开发语言·python·开源·beautifulsoup·virtualenv·pandas·pip
MoRanzhi12031 天前
15. Pandas 综合实战案例(零售数据分析)
数据结构·python·数据挖掘·数据分析·pandas·matplotlib·零售
eqwaak02 天前
数据预处理与可视化流水线:Pandas Profiling + Altair 实战指南
开发语言·python·信息可视化·数据挖掘·数据分析·pandas
Love__Tay2 天前
【数据分析与可视化】2025年一季度金融业主要行业资产、负债、权益结构与增速对比
金融·excel·pandas·matplotlib
万粉变现经纪人3 天前
如何解决 pip install -r requirements.txt 约束文件 constraints.txt 仅允许固定版本(未锁定报错)问题
开发语言·python·r语言·django·beautifulsoup·pandas·pip
万粉变现经纪人3 天前
如何解决 pip install -r requirements.txt 无效可编辑项 ‘e .‘(-e 拼写错误)问题
开发语言·python·r语言·beautifulsoup·pandas·pip·scipy
MoRanzhi12033 天前
12. Pandas 数据合并与拼接(concat 与 merge)
数据库·人工智能·python·数学建模·矩阵·数据分析·pandas
MoRanzhi12034 天前
11. Pandas 数据分类与区间分组(cut 与 qcut)
人工智能·python·机器学习·数学建模·分类·数据挖掘·pandas