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

相关推荐
F_D_Z2 天前
DataFrame中.iloc 属性
pandas·dataframe·.iloc
husterlichf3 天前
pandas__unstack方法与set_index详解
数据挖掘·数据分析·pandas
wudl55666 天前
Pandas-之数据可视化
信息可视化·数据分析·pandas
万粉变现经纪人6 天前
如何解决 pip install 安装报错 [WinError 32] 文件被占用(杀毒/占用进程)问题
python·pycharm·flask·beautifulsoup·bug·pandas·pip
文人sec7 天前
使用python-pandas-openpyxl编写运营查询小工具
开发语言·python·pandas
咋吃都不胖lyh7 天前
比较两个excel文件的指定列是否一致
爬虫·python·pandas
IT小哥哥呀9 天前
Python实用技巧:批量处理Excel数据并生成销售报表(含实战案例)
python·pandas·数据可视化·数据处理·报表生成·excel自动化·办公神器
Serendipity_Carl10 天前
爬虫数据清洗可视化链家房源
python·pandas·matplotlib
husterlichf10 天前
pandas___get_dummies详解
pandas