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

相关推荐
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
njxiejing1 天前
Pandas数据结构(DataFrame,字典赋值)
数据结构·人工智能·pandas
Calihen的学习日志2 天前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
Source.Liu3 天前
【Python自动化】 21.2 Pandas 读取 Excel 时的 dtype 参数完全指南
python·自动化·pandas
Source.Liu3 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
Source.Liu3 天前
【Python自动化】 21.1 Pandas 读取 Excel 文件的完整指南
python·自动化·pandas
偷心伊普西隆5 天前
Pandas DataFrame 指南
python·数据分析·pandas
chad__chang10 天前
Pandas的数据结构
数据结构·pandas
老歌老听老掉牙13 天前
Pandas DataFrame 列数操作完全指南
python·pandas
万粉变现经纪人13 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘websockets’问题
ide·pycharm·beautifulsoup·pandas·fastapi·pip·httpx