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

相关推荐
万粉变现经纪人2 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘tkinter’问题
python·beautifulsoup·pandas·pip·策略模式·httpx·scipy
超龄超能程序猿6 天前
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
人工智能·python·机器学习·numpy·pandas·scipy
李昊哲小课7 天前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas
袁袁袁袁满7 天前
利用Pandas进行条件替换与向前填充
pandas·条件替换·向前填充
Deng9452013148 天前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术
liuweidong08028 天前
【Pandas】pandas DataFrame from_records
pandas
大数据魔法师9 天前
基于Pandas和FineBI的昆明职位数据分析与可视化实现(五) - 基于随机森林算法预测职位分类
算法·pandas
Chasing__Dreams10 天前
python--杂识--18.1--pandas数据插入sqlite并进行查询
python·sqlite·pandas
超龄超能程序猿11 天前
(3)机器学习小白入门 YOLOv: 解锁图片分类新技能
python·numpy·pandas·scipy
好开心啊没烦恼12 天前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas