Skip to content

Instantly share code, notes, and snippets.

@do-me
Created October 1, 2024 18:49
Show Gist options
  • Save do-me/3641762df256e5ffdcc80c63654f7cb8 to your computer and use it in GitHub Desktop.
Save do-me/3641762df256e5ffdcc80c63654f7cb8 to your computer and use it in GitHub Desktop.
Convert array to string with fixed decimal precision, multiprocessing with pandarallel based on pandas df
import json
import numpy as np
from pandarallel import pandarallel
pandarallel.initialize(progress_bar=True)
# Function to round array to 2 decimal places and serialize to JSON
def round_and_serialize(x):
if isinstance(x, np.ndarray):
# Round and format each number to 2 decimal places
rounded_array = [round(float(num), 2) for num in x]
return json.dumps(rounded_array) # Serialize to JSON
else:
return json.dumps(x)
# Apply the function in parallel
sample["embeddings"] = sample["embeddings"].parallel_apply(round_and_serialize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment