Issue
I'm read a table from website using df = pd.read_html('website link')
:
df = pd.read_html('w3schools.com/python/python_ml_decision_tree.asp')
df[0]
It successfully read the table but I want to replace the 1st row as the header. I'm using this code:
df.columns = df.iloc[0]
df = df[1:]
df.head()
but it gave me an error that said:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-f9b2cba2eb0b> in <module>
----> 1 df.columns = df.iloc[0] #grab the first row for the header
2 df = df[1:] #take the data less the header row
3 df
AttributeError: 'list' object has no attribute 'iloc'
Solution
Try this:
df = pd.read_html('https://www.w3schools.com/python/python_ml_decision_tree.asp')
df[0].columns = df[0].iloc[0]
df = df[0][1:]
Answered By - Luis Alejandro Vargas Ramos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.