Home > Software engineering >  How to iterate over a pandas dataframe to assign values in specific columns to a string
How to iterate over a pandas dataframe to assign values in specific columns to a string

Time:09-17

I'm having problems figuring out the correct code to iterate over a pandas dataframe in order to assign values in specific columns to values in a dictionary.

I used pandas to read csv file to get the df below:

dataframe pic

     badge_id  case_id case_type description  short_des
0    123      1690     type0     subtype4        xxx
1    456      1717     type0     subtype2        yyy
2    789      1389     type0     subtype2        aaa
3    231      1540     type3     subtype1        bbb

I would like to iterate over the df to assign values in columns badge_id, case_id, and short_desc to the following string.

"{'opened_for': { value: **df[badge_id]**},'short_description': { value : **df['short_desc]**}, 'case_id': { value : **df[case_id]**}}"

I tried assigning the data in each column to a variable by appending the data in each column to an empty list. Then I used a for loop to iterate through the rows and list variables, but I can't seem to get the data from all three columns into their own designated strings.

Given the above df, the ideal output is 4 strings (or more depending on how many rows of data there are in the df):

"{'opened_for': { value: **'123'**},'short_description': { value : **'xxx'**}, 'case_id': { value : **1690**}}"

"{'opened_for': { value: **'456'**},'short_description': { value : **'yyy'**}, 'case_id': { value : **'1717'**}}"

"{'opened_for': { value: **'789'**},'short_description': { value : **'aaa'**}, 'case_id': { value : **'1389'**}}"

"{'opened_for': { value: **'231'**},'short_description': { value : **'bbb'**}, 'case_id': { value : **'1540'**}}"

CodePudding user response:

You can use list-comprehension df.iterrows():

out = [
    {
        "opened_for": {"value": row["badge_id"]},
        "short_description": {"value": row["short_des"]},
        "case_id": {"value": row["case_id"]},
    }
    for _, row in df.iterrows()
]
print(out)

Prints:

[
    {
        "opened_for": {"value": 123},
        "short_description": {"value": "xxx"},
        "case_id": {"value": 1690},
    },
    {
        "opened_for": {"value": 456},
        "short_description": {"value": "yyy"},
        "case_id": {"value": 1717},
    },
    {
        "opened_for": {"value": 789},
        "short_description": {"value": "aaa"},
        "case_id": {"value": 1389},
    },
    {
        "opened_for": {"value": 231},
        "short_description": {"value": "bbb"},
        "case_id": {"value": 1540},
    },
]
  • Related