It's slightly embarassing to ask for an excel functionality in python but I need help finding an efficent way to do this so I can breakaway from excel in this project.
For me an advantage in excel is a table which the columns all update based on input in another columns. One way I imagined to do this is in functions:
def update_columns(x, y):
mt = x * y
sm = x + y
sb = x - y
dv = x / y
cols = {'x' : x, 'y' : y, 'mt' : mt, 'sm' : sm, 'sb' : sb, 'dv' : dv}
return cols
df = pd.DataFrame(columns=['x', 'y', 'mt', 'sm', 'sb', 'dv'])
new_vals = [(10, 5), (15, 3), (40, 4)]
for i in new_vals:
x, y = i
cols = update_columns(x, y)
df = df.append(cols, ignore_index=True)
However, I imagine there being an eleganter, more pythonic way of going about this. Is it possible to use embedded lambda functions in a list or dict? Or some collection functions such as named tupels, or a data class / attr class? Could someone please suggest something?
question from:
https://stackoverflow.com/questions/65886227/update-columns-in-pandas-dataframe-like-excel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…