I have a pandas
data frame like this,
Name Not_Included Quantity Not_Included
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS
Now, I want to rename Not_Included
using data frame's column indexes. So, I get the output like this,
Name Not_Included_1 Quantity Not_Included_3
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS
I tried the following,
for c,v in enumerate(s_df):
if v == 'Not_Included':
vi = 'Not_Included' + str(c)
s_df.rename(columns=lambda n: n.replace(v, vi), inplace=True)
I get the following result,
Name Not_Included31 Quantity Not_Included31
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS
There are posts to rename a whole data frame's columns, but that is not what I am looking for since I am automating some tasks. How can I get my desired output using index of columns?
Also, can I do it in list comprehension method in renaming pandas columns?
Any ideas would be great.