Sample Data:
df1 = pd.DataFrame([
{'ID':'0001', 'Term':'Fall', 'Amount':1000},
{'ID':'0002', 'Term':'Fall', 'Amount':2000},
{'ID':'0001', 'Term':'Spring', 'Amount':50},
{'ID':'0002', 'Term':'Spring', 'Amount':50},
{'ID':'0001', 'Term':'Summer', 'Amount':200},
{'ID':'0002', 'Term':'Summer', 'Amount':200},
])
Output:
ID Term Amount
0 0001 Fall 1000
1 0002 Fall 2000
2 0001 Spring 50
3 0002 Spring 50
4 0001 Summer 200
5 0002 Summer 200
我想为条件为“春季”的每一行更新金额列。 “金额”列应设置为与同一ID对应的金额值,但适用于“秋季”期限。
任何指导将不胜感激。
Desired output:
ID Term Amount
0 0001 Fall 1000
1 0002 Fall 2000
2 0001 Spring 1000
3 0002 Spring 2000
4 0001 Summer 200
5 0002 Summer 200
答案 0 :(得分:1)
这是一种方法,其中我们将每个术语旋转到一列中,仅更新Spring
列,然后将melt
更新回原始结构。
df1 = df1.pivot_table("Amount", "ID", "Term").reset_index()
df1["Spring"] = df1["Fall"]
df1 = df1.melt("ID", value_name="Amount")
这是即使还有其他列也应该起作用的另一种方法:
df_spring = df1.loc[df1["Term"] == "Spring", ["ID", "Term"]]
df_fall = df1.loc[df1["Term"] == "Fall", ["ID", "Amount"]]
df_spring = df_spring.reset_index().merge(df_fall).set_index("index")
df1.update(df_spring)
答案 1 :(得分:0)
您可以执行以下操作:
import pandas as pd
df1 = pd.DataFrame([
{'ID':'0001', 'Term':'Fall', 'Amount':1000},
{'ID':'0002', 'Term':'Fall', 'Amount':2000},
{'ID':'0001', 'Term':'Spring', 'Amount':50},
{'ID':'0002', 'Term':'Spring', 'Amount':50},
])
number_rows = list(range(len(df1.index)))
print(number_rows)
for n in number_rows:
if df1.at[n, "Term"] == "Spring":
for i in number_rows:
if df1.at[i, "ID"] == df1.at[n, "ID"]:
df1.at[n, "Amount"] = df1.at[i, "Amount"]
print(df1)
输出:
ID Term Amount
0 0001 Fall 1000
1 0002 Fall 2000
2 0001 Spring 1000
3 0002 Spring 2000
答案 2 :(得分:0)
您可以在弹簧所在的列ID中使用map
,选择带有Fall的行并在set_index
中选择ID。
mSpring = df1['Term'].eq('Spring')
df1.loc[mSpring, 'Amount'] = (df1.loc[mSpring, 'ID']
.map(df1.loc[df1['Term'].eq('Fall')]
.set_index('ID')['Amount'])
)
print(df1)
ID Term Amount
0 0001 Fall 1000
1 0002 Fall 2000
2 0001 Spring 1000
3 0002 Spring 2000
4 0001 Summer 200
5 0002 Summer 200