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