rain123
rain123

Reputation: 273

Why my for loop is overwriting the variable instead appending?

I am trying to convert multiple columns from float into string type from a dataframe.

I have a dataframe below:

df = pd.DataFrame({'User':['101','102','103','104','105','106'],'CountA':[7,8,9,10,11,12],'CountB':[1,2,3,4,5,6],'CountC':[13,14,15,16,17,18]})

I am trying to convert column "CountA" and "CountB" into string using for loop:

convert = (df["CountA"], df["CountB"])
col_name = ["CountA","CountB"]
for a in convert:
       for b in col_name:
           df[b] = a.astype(str)

After converting using for loop my CountA value replaced by CountB value as below:

df = pd.DataFrame({'User':['101','102','103','104','105','106'],'CountA':[1,2,3,4,5,6],'CountB':[1,2,3,4,5,6],'CountC':[13,14,15,16,17,18]})

What has happened?

Upvotes: 0

Views: 61

Answers (2)

1__
1__

Reputation: 1571

In your loop, you are changing their places twice. The first time, it makes the right replacement. The second iteration switches them.

Use this one instead:

for a,b in zip(convert,col_name):
    df[b] = a.astype(str)

Or an easier option is this:

df[["CountA","CountB"]] = df[["CountA","CountB"]].astype(str)

Upvotes: 2

Kristada673
Kristada673

Reputation: 3744

If all you want to do is change the data type of the columns from int to string, why not simply do the following:

col_name = ["CountA","CountB"]
for col in col_name:
    df[col] = df[col].astype('str')

Upvotes: 1

Related Questions