我有一个大数据框(data_df),并创建了四个新列。假设原始列为“ A”,“ B”,“ C”,“ D”,四个新列为“ E”,“ F”,“ G”,“ H”。对于每一行(3000),我需要将值添加到新列中。在“ E”中,必须为A / A + B。对于“ F” B / A + B。对于“ G” C / C + D。对于“ H” D / C + D。我需要为每行运行一次。
答案 0 :(得分:1)
这很简单/直观。类似的问题是here,下面是您的特定问题的答案。
const ajv = require('ajv');
const ajvKeywords = require('ajv-keywords');
const ajvInstance = new ajv(options);
ajvKeywords(ajvInstance, ['transform']);
import pandas as pd
### Make up data for example
A = [5, 8, 2, -1]
B = [1, 0, 1, 3]
df = pd.DataFrame(list(zip(A,B)), columns =['A', 'B'])
display(df)
答案 1 :(得分:1)
针对您的特定问题
import pandas
df = pandas.DataFrame({'A':[1,2], 'B':[3,4],'C':[5,6], 'D':[7,8]})
df['E'] = df.apply(lambda row: row.A/(row.A + row.B), axis=1)
df['F'] = df.apply(lambda row: row.B/(row.A + row.B), axis=1)
df['G'] = df.apply(lambda row: row.C/(row.C + row.D), axis=1)
df['H'] = df.apply(lambda row: row.D/(row.C + row.D), axis=1)
print(df)
输出
A B C D E F G H
1 3 5 7 0.250000 0.750000 0.416667 0.583333
2 4 6 8 0.333333 0.666667 0.428571 0.571429