Atom Store
Atom Store

Reputation: 1006

How to use while loop properly?

I have a dataframe:

SIN1       SIN2      SIN3
4778       5633      4343

I have tried,

count_col = len(df1.columns)

check=1

while check<=count_col:
    check_str = str(check)
    check_for_column = "SIN"+check_str in df1
    name = "SIN"+check_str
    
    if check_for_column == True:
        df1[name] = df1[name].astype(str)
        df1['SIN1'] = df1['SIN1'] + ',' + df1[name]

    if check == count_col:
        break
    
    check += 1

df1[['SIN1']]

This shows 4778,4343,4778,4343.................

When I tried,

check=1

while check<=count_col:
    check_str = str(check)
    check_for_column = "SIN"+check_str in df1
    name = "SIN"+check_str
    
   

    if check == count_col:
        break
    
    check += 1

if check_for_column == True:
     df1[name] = df1[name].astype(str)
     df1['SIN1'] = df1['SIN1'] + ',' + df1[name]

df1[['SIN1']]

This shows 4778,4343

I want the result to be, 4778,5633,4343

Please don't suggest a way to directly concatenate with ','.

I used while loop because there can be any no of SIN columns so.

How to properly use while loop in this case?

Upvotes: 0

Views: 131

Answers (2)

Corralien
Corralien

Reputation: 120499

Use apply to join column values:

>>> df['SIN'] = df.astype(str).apply(lambda x: ','.join(x), axis=1)
>>> df

   SIN1  SIN2  SIN3             SIN
0  4778  5633  4343  4778,5633,4343

To select a subset of columns like SINxx, use filter:

df.filter(like='SIN')  # or df.filter(regex='SIN\d+')

Upvotes: 1

bicarlsen
bicarlsen

Reputation: 1371

This code takes all columns with SIN in them, concatenates the values as a string, and assigns it to a new column SIN.

sin_cols = [ ( 'SIN' in col ) for col in df.columns ]
sdf = df.loc[ :, sin_cols ]
df[ 'SIN' ] = sdf.apply( lambda x: ', '.join( x.values.astype( str ) ), axis = 1 )

df before

id T SIN1 SIN2 Q SIN3
0 8 3 6 9 8
1 1 6 1 7 1
2 5 2 4 8 6

df after

id T SIN1 SIN2 Q SIN3 SIN
0 8 3 6 9 8 3, 6, 8
1 1 6 1 7 1 6, 1, 1
2 5 2 4 8 6 2, 4, 6

Upvotes: 0

Related Questions