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

相关推荐
XYX的Blog15 小时前
Pandas基础07(Csv/Excel/Mysql数据的存储与读取)
mysql·excel·pandas
golitter.18 小时前
pandas中的str使用方法
pandas
一名技术极客3 天前
Python 数据分析 - 初识 Pandas
python·数据分析·pandas
生信与遗传解读3 天前
Pandas与Numpy的数据分析进阶题
数据分析·numpy·pandas
请为小H留灯4 天前
Python 数据清洗与处理常用方法全解析
大数据·python·jupyter·pandas
jcsx9 天前
证券量化交易选择合适的编程语言
javascript·servlet·numpy·pandas·pyqt
史嘉庆11 天前
Pandas 数据分析(二)【股票数据】
大数据·数据分析·pandas
liuweidong080211 天前
【Pandas】pandas Series rolling
pandas
史嘉庆12 天前
Pandas数据分析 【Series | DataFrame】
python·数据挖掘·数据分析·pandas
叫我:松哥14 天前
基于python的财务数据分析与可视化设计与实现
大数据·python·数据挖掘·数据分析·pandas·matplotlib