[英]How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name?
I have two separate DataFrames:我有两个独立的 DataFrame:
df_a
df_b
The column names, type of data, and length are identical but the numbers are different.列名、数据类型和长度相同,但数字不同。 I want to subtract (df_a - df_b) and record that number in a new dataframe df_dif.
我想减去 (df_a - df_b) 并将该数字记录在新的 dataframe df_dif 中。 I can manage that okay with what I have below:
我可以用下面的内容来管理它:
colnames_in = ['col a', 'col b', 'col c']
colnames_out = ['new col a', 'new col b', 'new col c']
for i in range(len(colnames_in)):
df_dif[colnames_out[i] = df_a[colnames_in[i]] - df_b[colnames_in[i]]
Where I am stuck is trying to be less repetitious.我被困的地方是尽量减少重复。 How would i accomplish the same, but instead of listing out colnames_out as a list...just append the string 'new'?
我将如何完成同样的工作,而不是将 colnames_out 作为列表列出...只是 append 字符串“new”?
i think this should work:我认为这应该有效:
df_new = df_a.copy()
df_new.subtract(df_b)
IIUC, you can use pandas.add_prefix
for that: IIUC,您可以为此使用
pandas.add_prefix
:
colnames_in = ['col a', 'col b', 'col c']
df_a = pd.DataFrame([[10, 20, 30], [40, 50, 60], [70, 80, 90]], columns=colnames_in)
df_b = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=colnames_in)
df_diff = df_a.subtract(df_b).add_prefix('new ')
Output: Output:
new col a new col b new col c
0 9 18 27
1 36 45 54
2 63 72 81
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.