Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dictionary from two columns pandas

pd.Series(df.A.values,index=df.B).to_dict()
Comment

convert 2 columns to dictionary pandas

dict(zip(df.a,df.b))
Comment

create dict from two columns pandas

In [6]: df = pd.DataFrame(randint(0,10,10000).reshape(5000,2),columns=list('AB'))

In [7]: %timeit dict(zip(df.A,df.B))
1000 loops, best of 3: 1.27 ms per loop

In [8]: %timeit pd.Series(df.A.values,index=df.B).to_dict()
1000 loops, best of 3: 987 us per loop
Comment

python how to create dict from dataframe based on 2 columns

In [9]: pd.Series(df.Letter.values,index=df.Position).to_dict()
Out[9]: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
Comment

how to create dictionary between two columns in python

1
2
3
4
5
6
7
dict(zip(df.state, df.name))
 
{'AK': 'Alaska',
 'AL': 'Alabama',
 'AR': 'Arkansas',
 'AZ': 'Arizona',
 'CA': 'California'}
Comment

dict column to be in multiple columns python

In [2]: df = pd.DataFrame({'a':[1,2,3], 'b':[{'c':1}, {'d':3}, {'c':5, 'd':6}]})

In [3]: df
Out[3]:
   a                   b
0  1           {u'c': 1}
1  2           {u'd': 3}
2  3  {u'c': 5, u'd': 6}

In [4]: df['b'].apply(pd.Series)
Out[4]:
     c    d
0  1.0  NaN
1  NaN  3.0
2  5.0  6.0
Comment

pandas create dataframe from multiple dictionaries

sales = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140},
         {'account': 'Alpha Co',  'Jan': 200, 'Feb': 210, 'Mar': 215},
         {'account': 'Blue Inc',  'Jan': 50,  'Feb': 90,  'Mar': 95 }]
df = pd.DataFrame(sales)
Comment

PREVIOUS NEXT
Code Example
Python :: python check operating system 
Python :: get active window title python 
Python :: add column as index pandas 
Python :: check pip version 
Python :: python choose random sample from list 
Python :: python console animation 
Python :: timestamp change python 
Python :: tkinter python may not be configured for Tk 
Python :: pyplot define plotsize 
Python :: edge driver selenium python 
Python :: traceback python 
Python :: how to loop the length of an array pytoh 
Python :: how to detect a keypress tkinter 
Python :: pydrive list folders 
Python :: add horizontal line plotly 
Python :: text to speech python 
Python :: fraction thesis 
Python :: python3 as default python path macos 
Python :: draw bounding box on image python cv2 
Python :: Python sort dataframe by list 
Python :: python day number from date 
Python :: python is letter or number functin 
Python :: matplotlib wrap title 
Python :: convert pascal annotation to yolo 
Python :: python WhatsApp messaging spammer 
Python :: summation django queryset 
Python :: pandas df remove index 
Python :: today date python 
Python :: py check discord token 
Python :: Sin , Cos Graph using python turtle. 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =