Hi everyone
I don't know how to use Python
Attachment can print latitude and Longitude based on zipcode
I tried to add df= and Alteryx.write(df,1)
The error I get is "currently only panda dataframes can be used to pass data to out going connection in Alteryx"
Any thought?
Thanks
Solved! Go to Solution.
Hi @Chao_Ma,
Like the error message says, in order to write data out from the Python tool the data needs to be in the structure of a pandas DataFrame - this is simply a tabular data structure (like an excel spreadsheet) that matches how Alteryx represents data. It looks like currently your 'location' variable is a geopy location object. If you are trying to write out the latitude and longitude contained in your location object, you can do so by adding those values to a pandas DataFrame:
# put lat and long values from location object into a pandas dataframe
df = pd.DataFrame.from_records([[location.latitude, location.longitude]],
columns=["Latitude", "Longitude"])
# write out pandas dataframe
Alteryx.write(df, 1)
To use this code, you will want to make sure to import the pandas package as 'pd' at the top of your script. I've attached a full working example of your script to the post.
You can read more about working with pandas DataFrames here:
Code Friendly Structures: DataFrames in Python (Pandas)
Please let me know if you have any further questions!
Sydney
Hi @Chao_Ma,
There is definitely a way, you'll just need to learn a little Python 🙂🙂🙂🙂🙂
😊😊
😊
😊
😊
😊
😊
🙂
🙂
You'll want to read in your data to the Python tool using the Alteryx.read() function, and then iterate through each row of each of your selected columns, applying the geopy code you've worked with for each column/row, and then writing out the latitude and longitude for each resulting location object into a new dataframe. For iterating through a pandas dataframe, you might want to check out these StackOverflow discussions:
https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas
https://stackoverflow.com/questions/45670242/loop-through-dataframe-one-by-one-pandas
For help getting up and running with the Python tool and pandas:
https://community.alteryx.com/t5/Alteryx-Designer-Knowledge-Base/Tool-Mastery-Python/ta-p/197860
Thanks!
Iterating through pandas dataFrame objects is generally slow. Pandas iteration beats the whole purpose of using DataFrame. It is an anti-pattern and is something you should only do when you have exhausted every other option. It is better look for a List Comprehensions , vectorized solution or DataFrame.apply() method.
Pandas DataFrame loop using list comprehension example
result = [(x, y,z) for x, y,z in zip(df['Name'], df['Promoted'],df['Grade'])]
User | Count |
---|---|
19 | |
15 | |
13 | |
9 | |
8 |