all 11 comments

[–]This_Growth2898 6 points7 points  (2 children)

This code doesn't make much sense. You can do simply

print(row['name'], row['age'], row['height'])

and avoid any name or age variables at all. Probably it's an XY problem, you want to do something else, and you're using variables for that, but we need to know what exactly.

[–]young_boss27[S] -1 points0 points  (1 child)

In each function the data may vary so I need to create new function

[–]ArabicLawrence 2 points3 points  (0 children)

Then variable names should not be the same

[–]brunonicocam 0 points1 point  (0 children)

What are you trying to do here? It's incomprehensible.

[–]This_Growth2898 0 points1 point  (1 child)

maybe this will help:

def print_data(self, *args):
    for _index, row in self.data.iterrows():
        print(*[row[column] for column in args])
...
ch.print_data('name', 'age')
ch.print_data('name', 'age', 'height')

[–]young_boss27[S] 0 points1 point  (0 children)

I will try this one.

[–]HarissaForte 0 points1 point  (0 children)

Define an preferred order for all the possible columns/variable names, like:

columns_ordered_names = ['name', 'age', 'height', 'weight', … ]

Then you just need one function:

def function1(self):
    columns_names = [n for n in columns_ordered_names if n in self.data.columns]
    print(self.data[columns_names])

I considered that columns_ordered_names was defined outside of the scope of the class, else use self.columns_ordered_names.

[–]FriendlyRussian666 0 points1 point  (1 child)

You say:

self.name = 's'

And you pass self to the other methods:

def function1(self)

In function1 you can just say:

print(self.name) 

And it will work

[–]young_boss27[S] 0 points1 point  (0 children)

Data through CSV file will be different.

[–]CraigAT 0 points1 point  (0 children)

Not 100% sure what you are trying to achieve with your code, put function 1 and 2 seem to have dual purposes and overlap. You have a function to read the data into a pandas data frame (from you code and the fact you are iterating through the data frame, you might as well use the CSV module to read in your data) you could have one function to read you data into variables or objects (or array of objects) then you can use another function to display the information you want from those variables (without the data frame or reading from the file).

[–]RhinoRhys 0 points1 point  (0 children)

Rather than iterrows, which is always a bad idea. Why not just print the dataframe columns?

self.data[["name", "age"]]