Dynamo+Python+Pandas not working

Hello everyone, thank you in advance for your availability. Unfortunately I can’t figure out how but this library doesn’t work.
I would like to read all the values ​​in an ordered array, so that I can extract my values ​​from the csv.
How could I proceed?

Hi Giulia,

Cant see what the error says here, however if you want to import libraries check out this thread.

1 Like

@Helmy0001
Thank you for your answer. The library was imported successfully. But as you can see from the example it does not show me the values ​​correctly in the list. I do not understand why.Do I need to enter additional instructions on Dynamo?

Hi,
a few comments

  • if you use the raw string it is not necessary to put the double \\

  • it is strongly recommended to use the dynamo Python directory to install packages (where the sdtlib is located)
    see this page
    and
    see this topic

  • the OUT variable that passes through the dynamo wrapper does not allow displaying a complete dataframe (try to convert to List),

some solutions to watch a dataframe

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import pandas as pd

d = {'col1': [1, 2, 3], 'col2': [3, 4, 5], 'id' : [1233, 3445, 7786]}
df = pd.DataFrame(data=d)
df = df.set_index('id')

# solution 1 to display in the console
print(df)
#
# solution 2 converting to a string reprensation at OUT variable
OUT = repr(df)

you can also convert to an array (without the index)

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import pandas as pd

d = {'col1': [1, 2, 3], 'col2': [3, 4, 5], 'id' : [1233, 3445, 7786]}
df = pd.DataFrame(data=d)
df = df.set_index('id')

OUT = df.to_numpy()
4 Likes

Another solution converting dataframe to HTML and display it in a Winform

dataframe to html

import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import pandas as pd

clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class Form_df(Form):
    def __init__(self, df):
        self.df = df
        self._html = df.to_html()
        self.InitializeComponent()
    
    def InitializeComponent(self):
        self._webBrowser1 = System.Windows.Forms.WebBrowser()
        self.SuspendLayout()
        # 
        # webBrowser1
        # 
        self._webBrowser1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right
        self._webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
        self._webBrowser1.Location = System.Drawing.Point(0, 0)
        self._webBrowser1.MinimumSize = System.Drawing.Size(20, 20)
        self._webBrowser1.Name = "webBrowser1"
        self._webBrowser1.DocumentText  = self._html
        self._webBrowser1.Size = System.Drawing.Size(678, 378)
        self._webBrowser1.TabIndex = 0
        
        # 
        # Form_df
        # 
        self.ClientSize = System.Drawing.Size(678, 378)
        self.Controls.Add(self._webBrowser1)
        self.Name = "Form14"
        self.Text = "DataFrame"
        self.ResumeLayout(False)


d = {'col1': range(20), 'col2': range(10, 30), 'col3': range(20, 40),'id' : [chr(i) for i in range(65, 85)]}
df = pd.DataFrame(data=d)
df = df.set_index('id')
df['Sum'] = df.iloc[:].sum(axis=1)
a=Form_df(df)
a.Show()
6 Likes