I have first dataframe dffieldnames
. it has only one column FIELD_NAME
FIELD_NAME
MKEY
reporting_entity
provision_amount
ORIG_country
RCA_check
EWA_check
I have 2nd dataframe dfdata
where all above rows are displayed as columns with actual values. so it looks like below. it could be case that field name in 1st dataframe may or may not present as column in 2nd dataframe. for example - EWA check is only present in 1st dataframe but not present in 2nd dataframe.
MKEY | reporting_entity |provision_amount | ORIG_country | RCA_check | adj_id
123 | 1234556 | 400.2344 | NE | | ADJ02020
254 | 8484849 | | NE | YES | ADJ84848
I want to add new column(name = VALUE) in dataframe dffieldname
. I will be using adj_id as filter -
if adj_id='ADJ02020',dataframe dffieldname
output will be -
FIELD_NAME | VALUES
MKEY | 123
reporting_entity | 1234556
provision_amount | 400.2344
ORIG_country | NE
RCA_check |
EWA_check |
if adj_id = 'ADJ84848',dataframe dffieldname
output will be -
FIELD_NAME | VALUES
MKEY | 254
reporting_entity | 8484849
provision_amount |
ORIG_country | NE
RCA_check | YES
EWA_check |
You can do this using the index.
Make FIELD_NAME
the index.
dffieldnames.set_index('FIELD_NAME', inplace=True)
Then use that index to match values to the index of the Series
created by squeeze()
from the specific row selected:
dffieldnames['VALUE'] = dfdata[dfdata['adj_id']=='ADJ02020'].squeeze()
dffieldnames.fillna('')
VALUE
FIELD_NAME
MKEY 123
reporting_entity 1234556
provision_amount 400.2344
ORIG_country NE
RCA_check
EWA_check
If you don't want FIELD_NAME to remain as the index you could reset the index:
dffieldnames.reset_index()
FIELD_NAME VALUE
0 MKEY 123
1 reporting_entity 1234556
2 provision_amount 400.2344
3 ORIG_country NE
4 RCA_check
5 EWA_check
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.