7

I have following code to load dataframe

import pandas as pd

ufo = pd.read_csv('csv_path')
print(ufo.loc[[0,1,2] , :])

which gives following output, see the structure of the csv

          City Colors Reported Shape Reported State             Time
0       Ithaca             NaN       TRIANGLE    NY   6/1/1930 22:00
1  Willingboro             NaN          OTHER    NJ  6/30/1930 20:00
2      Holyoke             NaN           OVAL    CO  2/15/1931 14:00

Now, I want to add an extra column based on existing column. I have a list which consist of indices of participating columns. It can be 0,1 or 0,2,3 or 1,2,3 anything.

I need to create it dynamically. I could come up with following

df1['combined'] = df1['City']+','+df1['State']

Putting index doesn't seem to work. I want to join those columns. using ','.join()

3 Answers 3

3

Assuming the data types of all the columns you want to join are str, you can use [] with integer to pick up the columns and use apply to join them:

df[[0,2,3]].apply(','.join, axis=1)

#0      Ithaca,TRIANGLE,NY
#1    Willingboro,OTHER,NJ
#2         Holyoke,OVAL,CO
#dtype: object
Sign up to request clarification or add additional context in comments.

Comments

3

If the list of indices is l, you can use pd.Series.cat:

df[df.columns[l[0]]].astype(str).str.cat([df[df.columns[i]].astype(str) for i in l[1: ]], sep=',')

Example

In [18]: df = pd.DataFrame({'a': [1, 2], 'b': [2, 'b'], 'c': [3, 'd']})

In [19]: df[df.columns[l[0]]].astype(str).str.cat([df[df.columns[i]].astype(str) for i in l[1: ]], sep=',')
Out[19]: 
0    1,2
1    2,b
Name: a, dtype: object

2 Comments

I believe this is the same: df[[l[0]]].str.cat(df[l[1:]], sep=',')
@mdurant Thanks. I suspect you might be right in principle, but your specific code (and some of its variants) simply don't run by me. The first problem I get is 'DataFrame' object has no attribute 'str' (but fixing this gives me other errors).
3
def dyna_join(df, positions):
    return pd.concat([df, df.iloc[:, positions].apply(','.join, 1).rename('new_col')], axis=1)


dyna_join(df, [0, -2])

enter image description here

1 Comment

and maybe add astype(str) if int or float column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.