Answers for "pandas dictionary"

22

pandas dataframe from dict

data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data)
Posted by: Guest on March-30-2020
2

pandas dataframe from dict

>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d
Posted by: Guest on June-12-2020
1

convert dict to dataframe

#Lazy way to convert json dict to df

pd.DataFrame.from_dict(data, orient='index').T
Posted by: Guest on April-29-2020
3

python how to create a pandas dataframe from a dictionary

# 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
Posted by: Guest on October-03-2020
108

python dictionary

#Creating dictionaries
dict1 = {'color': 'blue', 'shape': 'square', 'volume':40}
dict2 = {'color': 'red', 'edges': 4, 'perimeter':15}

#Creating new pairs and updating old ones
dict1['area'] = 25 #{'color': 'blue', 'shape': 'square', 'volume': 40, 'area': 25}
dict2['perimeter'] = 20 #{'color': 'red', 'edges': 4, 'perimeter': 20}

#Accessing values through keys
print(dict1['shape'])

#You can also use get, which doesn't cause an exception when the key is not found
dict1.get('false_key') #returns None
dict1.get('false_key', "key not found") #returns the custom message that you wrote 

#Deleting pairs
dict1.pop('volume')

#Merging two dictionaries
dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
dict1 #{'color': 'red', 'shape': 'square', 'area': 25, 'edges': 4, 'perimeter': 20}

#Getting only the values, keys or both (can be used in loops)
dict1.values() #dict_values(['red', 'square', 25, 4, 20])
dict1.keys() #dict_keys(['color', 'shape', 'area', 'edges', 'perimeter'])
dict1.items() 
#dict_items([('color', 'red'), ('shape', 'square'), ('area', 25), ('edges', 4), ('perimeter', 20)])
Posted by: Guest on May-20-2020
2

dicts python

thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict["model"]
print(x)
---------------------------------------------------------------------------
Mustang
Posted by: Guest on October-30-2020

Python Answers by Framework

Browse Popular Code Answers by Language