python, la mejor manera de convertir una serie pandas en un dataframe pandas
Tengo una serie de Pandas sf:
email
[email protected] [1.0, 0.0, 0.0]
[email protected] [2.0, 0.0, 0.0]
[email protected] [1.0, 0.0, 0.0]
[email protected] [4.0, 0.0, 0.0]
[email protected] [1.0, 0.0, 3.0]
[email protected] [1.0, 5.0, 0.0]
Y me gustaría transformarlo en el siguiente DataFrame:
index | email | list
_____________________________________________
0 | [email protected] | [1.0, 0.0, 0.0]
1 | [email protected] | [2.0, 0.0, 0.0]
2 | [email protected] | [1.0, 0.0, 0.0]
3 | [email protected] | [4.0, 0.0, 0.0]
4 | [email protected] | [1.0, 0.0, 3.0]
5 | [email protected] | [1.0, 5.0, 0.0]
Encontré una manera de hacerlo, pero dudo que sea la más eficiente:
df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)
30
4 answers
En lugar de crear 2 dfs temporales, puede pasarlos como parámetros dentro de un dict utilizando el constructor DataFrame:
pd.DataFrame({'email':sf.index, 'list':sf.values})
59
Author: EdChum,
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
2014-09-29 11:20:04
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
2014-09-29 11:20:04
To_frame():
Comenzando con la siguiente serie, df:
email
[email protected] A
[email protected] B
[email protected] C
dtype: int64
Utilizo to_frame para convertir la serie a DataFrame:
df = df.to_frame().reset_index()
email 0
0 [email protected] A
1 [email protected] B
2 [email protected] C
3 [email protected] D
Ahora todo lo que necesita es cambiar el nombre de la columna y nombrar la columna de índice:
df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
Su DataFrame está listo para un análisis posterior.
Actualización: Acabo de encontrar este enlace donde las respuestas son sorprendentemente similares a las mías aquí.
29
Author: Shoresh,
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-05-23 11:47:26
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-05-23 11:47:26
Una línea de respuesta sería
myseries.to_frame(name='list').reset_index()
2
Author: Mysterious,
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-08-29 05:50:02
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-08-29 05:50:02
Series_Name = pd.DataFrame (Series_Name)
-2
Author: Bhagabat Behera,
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-06-24 11:11:15
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-06-24 11:11:15