I have a DataFrame, namely df
, like:
Name Subject Score
0 Tom A 91
1 Bob B 92
2 Ali C 93
and a dictionary like:
exam_info = {
"exam_date": "2021-04-01",
"advisor": "Jim",
}
My goal DataFrame is to insert exam_date
and advisor
into each row of df
, resulting like:
exam_date advisor Name Subject Score
0 2021-04-01 Jim Tom A 91
1 2021-04-01 Jim Bob B 92
2 2021-04-01 Jim Ali C 93
I know following code can be working:
df.insert(0, 'advisor', exam_info['advisor'])
df.insert(0, 'exam_date', exam_info['exam_date'])
but in the real project, I have a number of df
s to insert
and the real exam_info
dictionary is also quite lengthy, thus there will be a bunch of blocks of df.insert(..)
in the code, which is not so elegant.
I also tried to change exam_info
into a helper DataFrame df_helper
like:
exam_date advisor
0 2021-04-01 Jim
and then use pd.concat([df_helper, df], axis=1)
, but the resulting DataFrame will only have exam_date
and advisor
in the first row, with the corresponding columns in other rows are all Nan
, like:
exam_date advisor Name Subject Score
0 2021-04-01 Jim Tom A 91
1 NaN NaN Bob B 92
2 NaN NaN Ali C 93
Please advice if any more elegant ways to concat both DataFrames so that the NaN
s are filled up with correct values.
Do you want this?
df = pd.concat([df, pd.DataFrame([xam_info])],axis=1).fillna(method='ffill')
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.