Debug School

rakesh kumar
rakesh kumar

Posted on

ValueError: All arrays must be of the same length

yesterday, when i run the commands for webscrapping using python i got error
My commands:

import pandas as pd
datas= pd.DataFrame({'president_name':president_names,'terms_of_office':term_of_office})
datas
Enter fullscreen mode Exit fullscreen mode

Error:

Image description

Image description

Reason

when i run this seprately president_name and terms_of_office

import pandas as pd
datas= pd.DataFrame({'president_name':president_names})
datas
Enter fullscreen mode Exit fullscreen mode

output
Image description

import pandas as pd
datas= pd.DataFrame({'terms_of_office':term_of_office})
datas
Enter fullscreen mode Exit fullscreen mode

output

Image description

see in above output length is different president_name 18 row while terms_of_office 14 row

Solution

president_na = []


for item in president:
    parts = item.split('\n')
    if len(parts) > 1:
        president_na.append(parts[0])      
president_na
Enter fullscreen mode Exit fullscreen mode
term_of = []

for item in president:
    parts = item.split('\n')
    if len(parts) > 1:

        term_of.append(parts[1])

term_of
Enter fullscreen mode Exit fullscreen mode
import pandas as pd
datas= pd.DataFrame({'president_name':president_na,'terms_of_office':term_of})
datas
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)