Saltar al contenido

from_dict pandas ejemplos de código de nombres de columnas

Ejemplo 1: convertir dict en marco de datos

#Lazy way to convert json dict to df

pd.DataFrame.from_dict(data, orient='index').T

Ejemplo 2: Python cómo crear un marco de datos de pandas a partir de un diccionario

# Basic syntax:
import pandas as pd
pandas_dataframe = pd.DataFrame(dictionary)
# Note, with this command, the keys become the column names 

# Create dictionary:
import pandas as pd
student_data = {'name' : ['Jack', 'Riti', 'Aadi'], # Define dictionary
    	   	    'age' : [34, 30, 16],
    		    'city' : ['Sydney', 'Delhi', 'New york']}

# Example usage 1:
pandas_dataframe = pd.DataFrame(student_data) 
print(pandas_dataframe)
	name	age	city	# Dictionary keys become column names
0	Jack	34	Sydney
1	Riti	30	Delhi
2	Aadi	16	New york

# Example usage 2:
# Only select listed dictionary keys to dataframe columns:
pandas_dataframe = pd.DataFrame(student_data, columns=['name', 'city'])
print(pandas_dataframe)
	name	city
0	Jack	Sydney
1	Riti	Delhi
2	Aadi	New york

# Example usage 3:
# Make pandas dataframe with keys as rownames:
pandas_dataframe = pd.DataFrame.from_dict(student_data, orient='index') 
print(pandas_dataframe)
           0      1         2
name    Jack   Riti      Aadi # Keys become rownames
age       34     30        16
city  Sydney  Delhi  New york
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *