Convertir spark DataFrame column a python list


Trabajo en un dataframe con dos columnas, mvv y count.

+---+-----+
|mvv|count|
+---+-----+
| 1 |  5  |
| 2 |  9  |
| 3 |  3  |
| 4 |  1  |

Me gustaría obtener dos listas que contengan valores mvv y el valor count. Algo así como

mvv = [1,2,3,4]
count = [5,9,3,1]

Así que probé el siguiente código: La primera línea debería devolver una lista de filas en python. Quería ver el primer valor:

mvv_list = mvv_count_df.select('mvv').collect()
firstvalue = mvv_list[0].getInt(0)

Pero recibo un mensaje de error con la segunda línea:

AttributeError: getInt

Author: nha, 2016-07-27

5 answers

Vea, por qué esta manera que usted está haciendo no está funcionando. En primer lugar, usted está tratando de obtener entero de un tipo de fila, la salida de su recoger es así:

>>> mvv_list = mvv_count_df.select('mvv').collect()
>>> mvv_list[0]
Out: Row(mvv=1)

Si tomas algo como esto:

>>> firstvalue = mvv_list[0].mvv
Out: 1

Obtendrá el valor mvv. Si quieres toda la información del array puedes tomar algo como esto:

>>> mvv_array = [int(i.mvv) for i in mvv_list.collect()]
>>> mvv_array
Out: [1,2,3,4]

Pero si haces esta fila debajo obtienes:

>>> count_array = [int(i.count) for i in mvv_list.collect()]
Out: TypeError: int() argument must be a string or a number, not 'builtin_function_or_method'

Esto sucede debido a que el método count es un método incorporado. Y la columna tiene el mismo nombre como conde. Una solución para hacer esto es cambiar el nombre de la columna de count a _count:

>>> mvv_list = mvv_list.selectExpr("mvv as mvv", "count as _count")

Y luego intenta hacer esto:

>>> mvv_array = [int(i.mvv) for i in mvv_list.collect()]
>>> mvv_count = [int(i._count) for i in mvv_list.collect()]

Y finalmente funciona!

Espero que eso te ayude.

 43
Author: Thiago Baldim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-27 13:56:48

Después de un trazador de líneas da la lista que desea.

mvv = mvv_count_df.select("mvv").rdd.flatMap(lambda x: x).collect()
 44
Author: Neo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-26 18:54:40

Esto le dará todos los elementos como una lista.

mvv_list = list(
    mvv_count_df.select('mvv').toPandas()['mvv']
)
 9
Author: Muhammad Raihan Muhaimin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-06 05:02:22

El siguiente código le ayudará

mvv_count_df.select('mvv').rdd.map(lambda row : row[0]).collect()
 3
Author: Itachi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-11 08:59:48

AttributeError: el objeto' list 'no tiene atributo'collect'

Si obtiene este error, use el siguiente código

Mvv_list = mvv_count_df.seleccione('mvv').collect ()

Mvv_array =[int (i. mvv) for i in mvv_list]

 1
Author: anirban sen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-07 09:46:40