I have a data frame which contains 34 rows and 10 columns. I called the data frame "comp" now I did "invcomp = 1/comp", So the values changed but column name will be same. I want to replace or rename my column names, suppose the earlier name of my first column was "Cbm_m" in "comp", now I want to convert it to "Cbm_m_inv" in "invcomp". Extending or adding an extra term in last.
Asked
Active
Viewed 535 times
-1
-
2Possible duplicate of [How to add a suffix to each column name?](https://stackoverflow.com/questions/34049618/how-to-add-a-suffix-to-each-column-name) – Zero Jun 24 '17 at 19:09
1 Answers
2
Use 'add_suffix':
invcomp = invcomp.add_suffix('_inv')
Setup:
invcomp = pd.DataFrame(pd.np.random.rand(5,5), columns=list('ABCDE'))
invcomp = invcomp.add_suffix('_inv')
Output:
A_inv B_inv C_inv D_inv E_inv
0 0.111604 0.016181 0.384071 0.608118 0.944439
1 0.523085 0.139200 0.495815 0.007926 0.183498
2 0.090169 0.357117 0.381938 0.222261 0.788706
3 0.802219 0.002049 0.173157 0.716345 0.182829
4 0.260781 0.376730 0.646595 0.324361 0.345097

Scott Boston
- 147,308
- 15
- 139
- 187
-
Thanks, I was trying to solve this from last 4 hours. Thanks a ton. Could I have your email or nay social media way of yours, so I can ask my further doubts directly to you. If it is okay with you. Overall Thanks a lot – Avanish Mishra Jun 24 '17 at 19:19
-
can you again help me, I have a data frame with 120 column now, I want to multiply them by each other. It is like generating 2D features, – Avanish Mishra Jun 25 '17 at 08:25
-
Please post this under a new question on Stack Overflow. And, I or someone else will help.you to the best of our knowledge. Thank you. Remember, we are freely volunteering our time, so post a minimal avout to solve your problem with input data and expected ourputs. – Scott Boston Jun 25 '17 at 08:35
-
@chrisb Hello I was using this ...: df = df_base.copy() ...: for pair in combinations(df.columns, 3): ...: new_col = '*'.join(pair) ...: df[new_col] = df[pair[0]] * df[pair[1]]*df[pair[2]] I was trying to muliply dataframe of 204 columns which is suppose to genrate 1394204 columns. But in 2 hours It only genrated 300420. Can shome how I make it faster. – Avanish Mishra Jul 05 '17 at 12:43