rename columns according to list

2024/11/16 18:49:41

I have 3 lists of data frames and I want to add a suffix to each column according to whether it belongs to a certain list of data frames. its all in order, so the first item in the suffix list should be appended to the columns of data frames in the first list of data frames etc. I am trying here but its adding each item in the suffix list to each column.

In the expected output

  • all columns in dfs in cat_a need group1 appended
  • all columns in dfs in cat_b need group2 appended
  • all columns in dfs in cat_c need group3 appended

data and code are here

df1, df2, df3, df4 = (pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('a', 'b')), pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('c', 'd')),pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('e', 'f')),pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('g', 'h')))cat_a = [df1, df2]
cat_b = [df3, df4, df2]
cat_c = [df1]suffix =['group1', 'group2', 'group3']
dfs = [cat_a, cat_b, cat_c]for x, y in enumerate(dfs):for i in y:suff=suffixi.columns = i.columns + '_' + suff[x]

thanks for taking a look!

Answer

Brian Joseph's answer is great*, but I'd like to point out that you were very close, you just weren't renaming the columns correctly. Your last line should be like this:

i.columns = [col + '_' + suff[x] for col in i.columns]

instead of this:

i.columns = i.columns + '_' + suff[x]
https://en.xdnf.cn/q/119160.html

Related Q&A

How to send a pdf file from Flask to ReactJS

How can I send a file from Flask to ReactJS? I have already code that in the frontend, the user upload a file and then that file goes to the Flask server, then in the flask server the file is modify, …

How to draw cover on each tile in memory in pygame

I am a beginner in pygame and I am not a English native speaker.My assignment is coding a game called Memory. This game contains 8 pairs pictures and an cover exists on each pictures. This week, our as…

Compare 2 Excel files and output an Excel file with differences

Assume for simplicity that the data files look like this, sorted on ID:ID | Data1 | Data2 | Data3 | Data4 199 | Tim | 55 | work | $55 345 | Joe | 45 | work | $34 356 | Sam |…

Problem to show data dynamically table in python flask

I want to show a table in the html using python flask framework. I have two array. One for column heading and another for data record. The length of the column heading and data record are dynamic. I ca…

RuntimeError: generator raised StopIteration

I am in a course and try to find my problem. I cant understand why if I enter something other than 9 digits, the if should raise the StopIteration and then I want it to go to except and print it out. W…

split list elements into sub-elements in pandas dataframe

I have a dataframe as:-Filtered_data[defence possessed russia china,factors driving china modernise] [force bolster pentagon,strike capabilities pentagon congress detailing china] [missiles warheads, d…

Image does not display on Pyqt [duplicate]

This question already has an answer here:Why Icon and images are not shown when I execute Python QT5 code?(1 answer)Closed 2 years ago.I am using Pyqt5, python3.9, and windows 11. I am trying to add a…

Expand the following dictionary into following list

how to generate the following list from the following dictionary d = {2: 4, 3: 1, 5: 3}f = [2**1,2**2, 2**3, 2**4, 3**1, 5**1, 5**2, 5**3, 2**1 * 3, 2**2 * 3, 2**3 * 3, 2**4 * 3, 5**1 * 3, 5**2 * 3, 5*…

how can I improve the accuracy rate of the below trained model using CNN

I have trained a model using python detect the colors of the gemstone and have built a CNN.Herewith Iam attaching the code of mine.(Referred https://www.kaggle.com) import os import matplotlib.pyplot a…

Classes and methods, with lists in Python

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class …