-1
$\begingroup$
project=pd.read_csv("train.csv",sep=',') 

this dataset train.csv has 200column, named as var_0,var_1 ....... var_199

for i in range(200):
    c= "var_" + str(i)

the above line is for creating variable names using loop

    #print(c)
    b= project.c.describe()

AttributeError                            Traceback (most recent call last)
<ipython-input-46-b90b661d528b> in <module>
      2     c= "var_" + str(i)
      3     #print(c)
----> 4     b= project.c.describe()

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5177             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5178                 return self[name]
-> 5179             return object.__getattribute__(self, name)
   5180 
   5181     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'c'
$\endgroup$
  • $\begingroup$ you have object c in for, not attribute for df.... $\endgroup$ – quester Oct 26 at 20:39
0
$\begingroup$

You are calling it as if c was an attribute, while it is a column name; it should be:

project[c].describe()

and you don't need to assign it to a variable.

$\endgroup$
0
$\begingroup$

You have two issues with your code.

1st problem You overwrite the variable c in every loop your for does

for i in range(200):
    c= "var_" + str(i)

>> print(c)
>> "var_200"

What you want is:

c = ["var_" + str(i) for i in range(200)]

or

c = []
for i in range(200):
    c.append("var_" + str(i))

2nd problem The c is a python variable. Not a column. What you need to do is to use it like this

project[c].describe()

What you did would work only if you had a dataframe with the column name "c". For example

    a    b    c
0   1    2    3
1   3    1    5

You would be able to use project.c and would print

    c
0   3
1   5
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.