Home > Net >  How to solve raise ValueError("columns must have matching element counts") ValueError: col
How to solve raise ValueError("columns must have matching element counts") ValueError: col

Time:12-22

a,b,c,d, and e are lists and they are of the same length

print(len(a),len(b),len(c),len(d),len(e))

results to:

2100,2100,2100,2100,2100

import pandas as pd

df = pd.DataFrame({'a':a,'b':b,'c':c,'d':d,'e':e})

df = df.explode(['c', 'd', 'e'],ignore_index=True)

df = df.fillna('')

df.to_csv("sampledata.csv")

This shows the error:

df = df.explode(['c', 'd', 'e'],ignore_index=True)
raise ValueError("columns must have matching element counts")
ValueError: columns must have matching element counts

Why is this showing error since they have all the same count?

CodePudding user response:

This should do the trick:

df.set_index(['a', 'b']).apply(pd.Series.explode).reset_index()
  • Related