Concat certain columns to one with no standard number of columns in each loop

976 views python
9

I have a dictionary:

#file1 mentions 2 columns while file2 mentions 3
dict2 = ({'file1' : ['colA', 'colB'],'file2' : ['colY','colS','colX'], etc..})

I want to do a concatenation of the mentioned columns in a new column for each file. This should be automated.

for k, v in dict1.items():
    df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(k, v)) #reads to a df
    df['new'] = df['first_col'].astype(str) + df['second_col'] etc.. #concatenation

How can I make this work every time, independent of the number of columns in each dictionary?

Simply to concatenate all the mentioned columns.

Example:

a = {'colA' : [123,124,112,165],'colB' :['alpha','beta','gamma','delta']}
file1 = pd.DataFrame(data = a)
file1

colA   colB
123    alpha
124    beta
112    gamma
165    delta

b = {'colY' : [123,124,112,165],'colS' :['alpha','beta','gamma','delta'], 'colX' :[323,326,378,399] }
file2 = pd.DataFrame(data = b)
file2

colY  colS      colX
123   alpha     323
124   beta      326
112   gamma     378
165   delta     399

Result:

file1

col_all
123 alpha
124 beta
112 gamma
165 delta

file2

call_all
123 alpha 323
124 beta  326
112 gamma 378
165 delta 399

answered question

1 Answer

4

I believe you need change:

df['new'] = df['first_col'].astype(str) + df['second_col']

to:

df['new'] = df.astype(str).apply(' '.join, axis=1)

posted this

Have an answer?

JD

Please login first before posting an answer.