Add_field adding values based on other column (aka conditional values)
See original GitHub issueCopying 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:
- Created 2 years ago
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi,
there are two options.
You can cast the data in the function:
Or you can normalize everything beforehand (less effective on large data):
Please re-open if you need more help 👍