简体   繁体   中英

Mapping python dictionary to pandas dataframe

Suppose I have a very large pandas dataframe.

(Pdb) >? responseDF
Empty DataFrame
Columns: [MTG_DEAL_NAME, CV_VOLATILITY_90D, TICKER, NAME, CRNCY, BASE_CRNCY, SETTLE_DT,...]

Now, suppose I also have a dictionary with only some of those values:

(Pdb) >? fieldvalues
{'NAME': 'MyName', 'CPN': '5', 'MATURITY': '2050-11-01', 'TICKER': 'MyComp'...},

Is there an easy way to insert the value of the dictionary into the appropriate column and leave the missing values as "NA" or something similar?

Do something like this

d={'NAME': 'MyName', 'CPN': '5', 'MATURITY': '2050-11-01', 'TICKER': 'MyComp'}
df=pd.concat([df,pd.Series(d).to_frame().T],axis=1)

Use append method:

import pandas as pd
df = pd.DataFrame({
    'A': [1],
    'B': [2]
})

d = {'A': 2}

df.append(d, ignore_index=True)
#     A   B
#0  1.0 2.0
#1  2.0 NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM