简体   繁体   English

我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名?

[英]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.

相关问题 如何使用for循环从Pandas DataFrame列进行追加? - How can I use a for loop to append from a Pandas DataFrame column? 如何将 Pandas DataFrame 的列转换为列表列表? - How can I convert columns of a pandas DataFrame into a list of lists? 如何在 pandas dataframe 中的列内操作列表名称 - How can I Manipulate list names inside columns in pandas dataframe 如何将列表/数组中的列填充到空的 Pandas dataframe 中,只有列名 - How to fill in columns from lists/arrays into an empty Pandas dataframe with only column names 无法使用 For 循环将 append 的 Dataframe 的列名添加到列表中 - Can't append Column Names of Dataframe To A List Using For Loop 如何将一列的Pandas数据框转换为两列的Pandas数据框? - How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns? 如何在 pandas dataframe 中使用字符串作为列名 - How to use string as column name in pandas dataframe 我如何以某种方式转置熊猫数据框,其中我只需要两列,一列用于标题 - How do i transpose a pandas Dataframe in a way, in which I am only gonna need two columns and one column is for the headline 将 Pandas 数据框中的两列展开为列表列表 - Unfurling two columns in a Pandas dataframe into a list of lists 如何在熊猫数据框中使用单引号将一列分为两部分? - How can I use a single quote to split one column into two within a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM