question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add_field adding values based on other column (aka conditional values)

See original GitHub issue

Copying from Discord channel, with some updated info:

Hi there! As Framework Frictionless documentation is still progress, I would like to ask whether someone can advice me regarding how to write custom step function? Coming from dataflows, my goal is to translate this to Framework:

def flow(parameters, datapackage, resources, stats):

    def category():
        def step(row):
            if row['field_1'] is not None:
                if float(row['field_1']) > 10:
                    row['Category'] = 'A'
                else:
                    row['Category'] = 'B '
        return step

    return Flow(
        add_field('Category', 'string'),
        category()
    )

In Framework I would use:

source = Resource(path="resource.csv")
target = transform(
    source,
    steps=[
        steps.field_add(name="field_1", type="string", value="A/B"), # <--HERE
    ]
)

Basically, question is how to make adding conditional value (as in this example “A” or “B”) depending on other column value from resource.Thanks.

UPDATE: I came with this, however I am not able to use comparison: if rec[‘field_1’] < 11 rather than if (rec[‘field_1’] in str(list(range(11))))

steps=[steps.field_add(name="Category",
                                     type='string', 
                                     value=lambda rec: 'A' if (rec['field_1'] in str(list(range(11)))) else 'B'),
                                     ]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rollcommented, Mar 25, 2021

Hi,

there are two options.

You can cast the data in the function:

source = Resource(path="inp.csv")
target = transform(
source,
steps=[
    steps.field_add(name="Category", type='string', function=lambda rec: '<100' if int(rec['BTOT'])>100 else '1-100'),])

Or you can normalize everything beforehand (less effective on large data):

source = Resource(path="inp.csv")
target = transform(
source,
steps=[
    spets.table_normalize(),
    steps.field_add(name="Category", type='string', function=lambda rec: '<100' if rec['BTOT']>100 else '1-100'),])
1reaction
rollcommented, Mar 24, 2021

Please re-open if you need more help 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sum a Column Based on Values in Another - Excel University
In this post, we'll learn how to add up a column of numbers based on the values in another column. For example, we...
Read more >
Sum a Column Based on Values in Another - YouTube
Learn Excel Fast:https://www.excel-university.com/ytIn this video, we'll learn how to add up a column of numbers based on the values in ...
Read more >
python - Pandas add column with value based on condition ...
Another method is by using the pandas mask (depending on the use-case where ) method. First initialize a Series with a default value...
Read more >
How to sum values based on criteria in another column in ...
In Excel, you can use formulas to quickly sum the values based on certain criteria in an adjacent column. 1. Copy the column...
Read more >
[Feature Proposal] Conditional fields (aka. hide/show ... - GitHub
[Feature][3.3] Add a toggle other field depending of the selected value of the based on the select2 value. #1257.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found