Python

leeloo_dallas
leeloo_dallas Member
edited February 2021 in Magic ETL

Hey guys,

I can't find any documentation on how to use the python widget.

I've read the documentation on the site already. It didn't provide me any insight on actually implementing it and the Schema tab is far too confusing. I don't even know why I would import all my selected columns if I'm going to be creating another dataframe.

I've gotten as far as importing my content but when it comes to implementing a code and previewing what it looks like, I get an error, "ValueError: domomagic: Argument must be a pandas.DataFrame with at least one column."

My script is the following. I've already added all my columns in the schema and imported my 'original' dataset. It says to "write a data frame so it's available to the next action" so I repeated the step for my 'index_o' and it doesn't work.

There are no nulls in my dataset, columns are numerical and nominal data.

# Import the domomagic package into the script 

from domomagic import *

from pandas import *

# read data from inputs into a data frame

original = read_dataframe('original')

# write your script here

index_o = acceptValFrame.index

# write a data frame so it's available to the next action

#write_dataframe(original)

write_dataframe(index_o)


I really wish there was a video going into this, I'm the kind of person that needs to see it in action, not just words, so long pages with just the very basics and no detail into applying the library available won't help me :(

Can someone provide examples that is beyond the documentation on domo with packages being applied?

Tagged:

Best Answer

  • GrantSmith
    GrantSmith Coach
    Answer ✓

    It depends if the index has a name or not. When you convert the Index to a dataframe the Index Name becomes the column name. I'm not certain how Domo would handle having an empty column name, likely it wouldn't like it so you might want to set the name on the index first if it isn't already set before converting it to a dataframe or you can rename the column on the dataframe after you've converted it.


    index_o = original.index
    index_o.name = 'My Column Name'
    write_dataframe(index_o.to_frame())
    

    Alternatively

    index_o = original.index
    df = index_o.to_frame()
    # must use a list with the same length as the number of columns in your dataframe
    df.columns = ['My Column Name']
    # alternatively you could use the rename method on the dataframe object.
    write_dataframe(df)
    

    I don't have access to the Python tiles in Domo so this is based off my experience with the Python language itself.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • Hi @user052846

    You've got the basic idea of what needs to happen however the one issue you have is that you're not writing back out a DataFrame object but rather an Index object. If you're wanting to export your index you need to convert it to a data frame using the to_frame() method first before attempting to write it back out to Domo.


    index_o = acceptValFrame.index
    df = index_o.to_frame()
    write_dataframe(df)
    


    The simple synopsis of the python tile is essentially

    1. Import Dataframe from Domo as a pandas DataFrame object
    2. Do Python / pandas stuff to the dataframe
    3. Export pandas Dataframe to Domo.


    This isn't really a Domo related issue but a pandas / Python issue. I'd highly recommend looking at the official pandas documentation (https://pandas.pydata.org/pandas-docs/stable/index.html) to get an understanding of how pandas works. There are also lots of pandas tutorial videos on YouTube as well if you're more of a visual learner. If you're more of a hands on type of learner you can utilize websites like datacamp.com

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • @user052846

    three things ... there are a couple products in domo that can use python, in the future it might be useful to include screen shots of your setup to establish context.


    given the error message you provided, is the preceding tile in your dataflow called 'original' ?

    # read data from inputs into a data frame
    original = read_dataframe('original')
    

    this is wrong.

    index_o = acceptValFrame.index
    

    you created a dataframe "original" and you're trying to run the method .index on something that hasn't been defined yet.


    corrected code:

    # Import the domomagic package into the script 
    
    from domomagic import *
    
    from pandas import *
    
    # read data from inputs into a data frame
    
    original = read_dataframe('original')
    
    # write your script here
    index_o = original.index
    
    # write a data frame so it's available to the next action
    
    #write_dataframe(original)
    
    write_dataframe(index_o)
    


    Jae Wilson
    Check out my 🎥 Domo Training YouTube Channel 👨‍💻

    **Say "Thanks" by clicking the ❤️ in the post that helped you.
    **Please mark the post that solves your problem by clicking on "Accept as Solution"
  • Ahh okay.

    So I tried the code snippit and I'm still getting an error.

    I've used Jupyter notebook and python numerous times at my previous job, so I'm not sure if it's the environment or I'm out of practice.

    I'm still getting that same error.

    I've added all columns to the schema too.

    Unfortunately Grant, telling me where to learn about how pandas works doesn't help with learning how to use it in a DOMO environment. These are practice runs. So I'm trying to gain an idea on how to use the tool.



  • Domo spins up a local environment / container for you to execute python code in. The only thing that Domo really controls is the read_dataframe and write_dataframe.


    If you take out the Domo aspect you can think of read_dataframe and write_dataframe similar to read_csv/write_csv or read_sql/write_sql. The only difference is the container will export the data from the Domo dataflow into a pandas dataframe instead of reading the dataframe from a CSV file or SQL query result.


    Regarding your error it's explicitly complaining about how you're attempting to write a dataframe but aren't passing in a dataframe object. You're passing in a pandas Index object instead (which you got from the .index property on your original dataframe). You need to convert your Index to a dataframe first. You can do this with the .to_frame() method on your index:

    write_dataframe(index_o.to_frame())
    
    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • @GrantSmith when you write the index_o we should give it an explicit column name first?

    Jae Wilson
    Check out my 🎥 Domo Training YouTube Channel 👨‍💻

    **Say "Thanks" by clicking the ❤️ in the post that helped you.
    **Please mark the post that solves your problem by clicking on "Accept as Solution"
  • GrantSmith
    GrantSmith Coach
    Answer ✓

    It depends if the index has a name or not. When you convert the Index to a dataframe the Index Name becomes the column name. I'm not certain how Domo would handle having an empty column name, likely it wouldn't like it so you might want to set the name on the index first if it isn't already set before converting it to a dataframe or you can rename the column on the dataframe after you've converted it.


    index_o = original.index
    index_o.name = 'My Column Name'
    write_dataframe(index_o.to_frame())
    

    Alternatively

    index_o = original.index
    df = index_o.to_frame()
    # must use a list with the same length as the number of columns in your dataframe
    df.columns = ['My Column Name']
    # alternatively you could use the rename method on the dataframe object.
    write_dataframe(df)
    

    I don't have access to the Python tiles in Domo so this is based off my experience with the Python language itself.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Ahhh thanks so much this makes a lot more sense to me.

  • @jaeW_at_Onyx yes. Otherwise it will cause it to be called something like “Unnamed 1” and you won’t know what that column contains. Either give the index a name before you convert it to a data frame or name the Column after

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**