[英]Python pandas: concat two DataFrames with different number of rows by duplication
I have a DataFrame, namely df
, like:我有一个 DataFrame,即
df
,如:
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:我的目标 DataFrame 是将
exam_date
和advisor
插入df
的每一行,结果如下:
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.但是在实际项目中,我要
insert
多个df
,而真正的exam_info
字典也很长,因此代码中会出现一堆df.insert(..)
块,不太优雅.
I also tried to change exam_info
into a helper DataFrame df_helper
like:我还尝试将
exam_info
更改为助手DataFrame df_helper
,例如:
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:然后使用
pd.concat([df_helper, df], axis=1)
,但生成的exam_date
将只有第一行有考试日期和advisor
,其他行中的相应列都是Nan
,如:
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.请建议是否有更优雅的方法来连接两个 DataFrame,以便
NaN
填充正确的值。
Do you want this?你想要这个吗?
df = pd.concat([df, pd.DataFrame([xam_info])],axis=1).fillna(method='ffill')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.