Search code examples
pythonpandasdataframehy

how to use hy to when operating on pandas dataframe columns


There seems to be no documentation on how to use hy on single column pandas operation such as the following. Would appreciate any help:

# simple instantiation to scalar
df['a'] = '2'
# the above can be done like so: (-> df (.assign :a "2")) but would appreciate any better ways

# cast a column to int
df['a'] = df['a'].astype(int)
# creating  derived columns
df['c'] = df['a'] + df['b']
#subsetting by columns
dd = df[['a','b']]
#subsetting by criteria
dd = df[(df['a'] > 1) & (df['b'] < 2)]

Solution

  • pandas doesn't actually change the syntax or semantics of Python itself; it just uses operator overloading. So you can use the Hy equivalents of the same operators without issues, although helper macros can make pandas a lot more convenient.

    ; simple instantiation to scalar
    (setv (get df "a") "2")
    ; cast a column to int
    (setv (get df "a") (.astype (get df "a") int))
    ; creating  derived columns
    (setv (get df "c") (+ (get df "a") (get df "c")))
    ;subsetting by columns
    (setv dd (get df ["a" "b"]))
    ;subsetting by criteria
    (setv dd (get df (& (> (get df "a") 1) (< (get df "b") 2))))