pandas增加列的七种方法总结
作者:小龙在山东
insert
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)
输出:
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
赋值
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)
或
df['C'] = df['A'].str.replace("A", "C") df['C'] = df['A'].map(lambda x: x.replace('A', 'C'))
输出:
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
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)
输出:
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
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
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)
结果
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
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)
输出:
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
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)
输出:
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
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)
结果:
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
到此这篇关于pandas增加列的七种方法总结的文章就介绍到这了,更多相关pandas增加列内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!