Run Python or JavaScript on a Dataset - Code Step for Scrapers
Pricing
from $0.16 / 1,000 items
Run Python or JavaScript on a Dataset - Code Step for Scrapers
Paste a Python or JavaScript function and run it on any Apify dataset, a JSON/CSV link or inline items. Dedupe, flatten, clean, group with pandas. Add it as an integration after your scraper.
Pricing
from $0.16 / 1,000 items
Rating
0.0
(0)
Developer
Northbell
Maintained by CommunityActor stats
0
Bookmarked
2
Total users
1
Monthly active users
2 days ago
Last modified
Categories
Features
Share
Paste a function, point it at data, get the result as a dataset. The Actor reads items from an Apify dataset, a JSON / JSON lines / CSV link, or items you type in, runs your Python or JavaScript on them, and saves what your function returns. Use it to dedupe, flatten, clean or group scraper output, and add it as an integration so it runs by itself after your scraper.
Try it now — the input is already filled in. Press Start. It runs a short JavaScript function on three example products: it drops the one without a title and adds a titleLength field to the others. In our own local run with this input: 3 items in, 2 items out, about 0.3 seconds of code time (the whole run took about 3 seconds). Cost: 2 returned items = $0.0004 (plus $0.00005 for the run start).
What you pay
| Event | Price | When |
|---|---|---|
item-out | $0.0002 | per item your code returns and the Actor saves ($0.20 per 1,000) |
apify-actor-start | $0.00005 | once per run |
- You pay for returned items only, not for items read. Dropping items is free.
- Items are saved and charged in chunks of 1,000. Set "Maximum total charge" on the run as your cap: the Actor stops cleanly when the next chunk does not fit, and everything charged is in the dataset.
- A run that fails before your code returns anything (empty code, no data, a code error) charges only the start event.
Run it after your scraper
- In Apify Console, open your scraper and go to Integrations.
- Add Run Actor (on "Run succeeded") and pick this Actor.
- Use an input like this:
{"language": "javascript","datasetId": "{{resource.defaultDatasetId}}","mode": "batch","code": "async function transform(items, ctx) {\n const seen = new Set();\n return items.filter((i) => i.url && !seen.has(i.url) && seen.add(i.url));\n}"}
Every run of the scraper hands over its dataset, your function runs on it, and the cleaned items appear in this Actor's own dataset (and can feed the next integration).
How your code looks
You write one function. ctx.input is the "Parameters" object from the input; ctx.log(...) writes to the run log.
| Mode | JavaScript (Node 24) | Python 3 |
|---|---|---|
batch | async function transform(items, ctx) { return items } | def transform(items, ctx): return items |
perItem | async function map(item, ctx) { return item } | def map(item, ctx): return item |
- Batch gets the whole list (up to "Max items", 100,000 by default) and returns a list of objects.
- Per item gets one item at a time and returns an object, a list of objects (to split an item) or
null/None(to drop it). Data is processed in chunks of 1,000, so memory stays small on big datasets. - In Python,
ctxis a dict:ctx["input"],ctx["log"]("text"). A pandas DataFrame returned fromtransformis turned into a list of rows; NaN becomesnull. - Every returned item must be a JSON object. If not, the run stops with a message that names the item index.
JavaScript: dedupe by URL
async function transform(items, ctx) {const seen = new Set();return items.filter((i) => i.url && !seen.has(i.url) && seen.add(i.url));}
JavaScript: flatten nested fields
async function map(item, ctx) {const { address, ...rest } = item;return { ...rest, city: address?.city ?? null, street: address?.street ?? null };}
Python: pandas groupby
Set "Python packages" to pandas if you want a specific version (pandas and numpy are already installed in the image), then:
import pandas as pddef transform(items, ctx):df = pd.DataFrame(items)return df.groupby("category", as_index=False).agg(count=("price", "size"), avg_price=("price", "mean"))
Python: flatten and clean per item
def map(item, ctx):price = item.get("price", {})if not item.get("title"):return None # dropreturn {"title": item["title"].strip(), "amount": price.get("amount"), "currency": price.get("currency")}
Where the data comes from
If more than one source is given, the order is: Dataset ID, then Source URL, then inline items.
- Dataset ID: any dataset you can read. In an integration use
{{resource.defaultDatasetId}}. - Source URL: a JSON array, a JSON lines file or a CSV file (comma, semicolon or tab; first line = field names; CSV values come as text). Only public http(s) links; private and local addresses are blocked.
- Items: a list of objects typed into the input.
Python packages
Add names to "Python packages" (at most 20, for example requests>=2.31). They are installed with pip install into a temporary folder at the start of the run, which can take from seconds to a few minutes and is not counted against the code time limit. The image is Alpine Linux. pandas and numpy are installed in the image from Alpine's own packages, so they work without compiling. Other packages that ship only source code or glibc wheels may fail to install on Alpine; the run then stops with pip's message before your code runs.
Security note
Your code runs in a separate, isolated child process inside your own Apify run. Nothing of the Actor's is passed to it except PATH, HOME, LANG, TZ, and your own APIFY_TOKEN and APIFY_USER_ID (so your code can call the Apify API as you, for example to start another Actor). No other environment variable is visible. Data goes to and from your code through temporary files that are deleted when the run ends. Your code can use the network and can do anything your token can do, so only run code you trust.
Limits
- Output items must be JSON objects; other return values stop the run with an error that names the item.
- "Code time limit" (default 300 seconds) covers all of your code in the run; when it runs out the process is killed and the run fails. Items saved before that stay saved (per item mode saves chunk by chunk).
- Memory: the run has 2048 MB by default. If your code runs out of memory the run fails with a plain message; use per item mode or raise the run memory.
- Batch mode keeps the whole list in memory (up to "Max items"). A Source URL file is read completely before processing (up to 200 MB).
- Per item mode starts your code once per chunk of 1,000 items, so state is not kept between chunks.
- JavaScript code is run as a CommonJS module: use
require(...)orawait import(...), notimportstatements. Only Node's built-in modules are available in JavaScript (no npm install). - Everything your code prints is shown in the run log (the first 100 lines).


