0

I have the following dataframe with certain numbers of columns.

id  column1 column2 column3 column4
0   X       A       P       S
1   Y
2   Z

user has to give the input via the .txt file which contains the parameters for the new columns which has to be added to the current dataframe. the .txt file has the following content.

{
    "new_case": {        
        "N": 2,        
        "concept:name": [
            "column1",
            "column3",
            "column4"
        ]
    }
}

The new column in the dataframe should contain the values from a mentioned column in the.txt file.

Expected output.

id  column1 column2 column3 column4 new_column
0   X       A       P       S       X+P+S
1   Y                               Y+Nan+Nan
2   Z                               Z+Nan+Nan

Any help is appreciated :) Thank you.

2
  • (1) Does the .txt file contain only a dictionary? (2) Are X, Y, Z etc. strings or are they placeholder for numbers? (3) Also what's the output for summation with NaN, i.e. what the output of Z+Nan+Nan? Commented Apr 20, 2022 at 5:21
  • @enke (1) Yes, the .txt file contains only a dictionary. (2) X, Y, Z, etc normally the strings but a few columns have integers values too. (3) output is concatenated values of columns mentioned under "concept:name". Thank you. Commented Apr 20, 2022 at 8:42

1 Answer 1

1

Just an example on your example data

import json
import pandas as pd


with open('input.txt', 'r', encoding='utf-8') as f:
    data = json.loads(f.read())

df['new_column'] = df[data['new_case']['concept:name']].fillna('Nan').agg('+'.join, axis=1)
print(df)

   id column1 column2 column3 column4 new_column
0   0       X       A       P       S      X+P+S
1   1       Y    <NA>    <NA>    <NA>  Y+Nan+Nan
2   2       Z    <NA>    <NA>    <NA>  Z+Nan+Nan
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answering. It works correctly. One question, How can I modify the code so that It concat the columns irrespective of data types.
@Syntax You can try df[data['new_case']['concept:name']].fillna('Nan').astype(str).agg('+'.join, axis=1).

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.