I want to add a punctuation mark at the end of every question in my dataframe. Each question is a column name. Since there are a lot of questions I decided to slice the dataframe using
df2.iloc[:, 59:125]
The code that I am using is:
df2.iloc[:, 59:125].add_suffix(".")
Ouput shows that this line does add a punctuation mark to the questions:
df2.iloc[:, 59:125].add_suffix(".")
Out[69]:
I would recommend this company as a great place to work . ...
What one change would make the biggest positive difference to you being able to perform your role effectively?.
but when I save it as an excel file and check, none of the 66 questions have been updated.
I have also tried:
[10270 rows x 66 columns]
df2.iloc[:, 59:125] = (df2.iloc[:, 59:125].add_suffix("."))
df2.iloc[:, 59:125] = (df2.iloc[:, 59:125].add_suffix("."))
print(df2.iloc[:, 59:125])
I would recommend this company as a great place to work ... What one change would make the biggest positive difference to you being able to perform your role effectively?
If you notice, there is no punctuation mark at the end of the questions displayed. Meaning that this code does not update the dataframe.
CodePudding user response:
You can enumerate
the column headers and reassign it if the header index is in desired range
lst = range(59, 125)
df = df.rename(columns={col: col '.'
for idx, col in enumerate(df.columns)
if idx in lst})
# or
df.columns = ['{}{}'.format(col, '.' if idx in lst else '')
for idx, col in enumerate(df.columns)]