Created
October 1, 2024 18:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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