serhii.net

In the middle of the desert you can say anything you want

09 Feb 2024

Pandas adding prefix to columns and making metadata out of column subsets

One can convert some column values to dict/json easily:

some_cols_subset = ['col1','col2']
df['my_metadata'] = df[some_cols_subset].apply(lambda x: to_dict(),axis=1)  #to_json()

To rename all cols by adding a prefix:

df[some_cols_subset].add_prefix("pr_")

# now these columns are called pr_col1, pr_col2

And of course both at the same time works as well:

tales[csv_md_target_key] = tales[other_cols].add_prefix(tale_metadata_prefix).apply(lambda x: x.to_dict(), axis=1)

# now that column contain a dict representation of the row, and I can later pass it as metadata to log somewhere where I don't want to drag pandas dataframes to, without manually creating dictionary
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus