pandas增加列的七种方法总结
目录
- insert
- 赋值
- loc
- concat
- apply、map
- map
- apply
- reindex
- assign
insert
def test1(): android 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 B02.0 A1 B13.0 A2 B2 A0 A B1.0 A00 A0 B02.0 A01 A1 B13.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 B02.0 A1 B13.0 A2 B2 A B C1.0 A0 B0 C02.0 A1 B1 C13.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 B02.0 A1 B13.0 A2 B2 A B C1.0 A0 B0 C02.0 A1 B1 C13.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']}, js index=[1.0, 2.0, 3.0]) print(df) df['C'] = df['A'].map(lambda x: x.replace('A', 'C')) print(df)VEjsbW
结果
A B
1.0 A0 B02.0 A1 B13.0 A2 B2 A B C1.0 A0 &nbs编程客栈p;B0 C02.0 A1 B1 C13.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 B02.0 A1 B13.0 A2 B2 A B C1.0 A0 B0 A0B02.0 A1 B1 A1B13.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 B02.0 A1 B13.0 A2 B2 A B C1.0 A0 B0 12.0 A1 B1 13.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 B02.0 A1 B13.0 A2 B2 A B C D1.0 A0 B0 A0B0 B0A02.0 A1 B1 A1B1 B1A13.0 A2 B2 A2B2 B2A2
到此这篇关于pandas增加列的七种方法总结的文章就介绍到这了,更多相关pandas增加列内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论