Python
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?
Best 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!**1
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
- Import Dataframe from Domo as a pandas DataFrame object
- Do Python / pandas stuff to the dataframe
- 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!**-1 -
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"0 -
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.
0 -
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!**1 -
@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"0 -
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!**1 -
Ahhh thanks so much this makes a lot more sense to me.
0 -
@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!**0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 753 Beast Mode
- 61 App Studio
- 41 Variables
- 692 Automate
- 177 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive