빅데이터(BigData)/Spark

toPandas() 후 조회 시 index 2 is out of bounds for axis 0 with size 에러가 발생할 때

leebaro 2021. 3. 16.
728x90

 

spark dataframe 또는 koalas를 이용해서 DF를 만들고 toPandas()를 이용해서 pandas DF로 변환해야하는 경우가 있다.

 

필자 같은 경우 DF로 heatmap을 만드는데 koalas DF에서 만들면 에러가 발생해서 pandas df로 변환했다.

 

문제는 변환 후 조회하면 "index 2 is out of bounds for axis 0 with size" 와 같은 에러가 발생했다.

 

구체적으로 DF에 NaN 값이 있었고, df.fillna(0)으로 NaN을 0값으로 변환한 경우에 에러가 발생했다.

 

이경우 toPandas() 코드 윗 부분에 아래와 같은 코드를 추가하면 된다. 파라미터를 -1로 하면 동일한 에러가 발생하는 것을 확인할 수 있다.

pd.set_option('display.max_columns', 0)

 


 

참고

 

pyspark toPandas() IndexError: index is out of bounds

I'm experiencing a weird behaviour of pyspark's .toPandas() method running from Jupyt. For example, if I try this: data = [{"Category": 'Category A', "ID": 1, "Value":...

stackoverflow.com

728x90