简体   繁体   English

Python pandas:通过复制连接两个具有不同行数的DataFrame

[英]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_dateadvisor插入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.

相关问题 Python Pandas - Concat两个具有不同行数和列数的数据帧 - Python Pandas - Concat two data frames with different number of rows and columns Pandas - 合并两个具有不同行数的数据帧 - Pandas - Merge two dataframes with different number of rows Python Pandas Concat 具有不同列和相同行的数据帧列表 - Python Pandas Concat list of Dataframes With Different Columns and Same Rows Pyspark:通过重复连接具有不同行数的 2 个数据帧 - Pyspark: Join 2 dataframes with different number of rows by duplication 在 pandas 中连接两个具有不同列的数据帧 - Concat two dataframes with different columns in pandas Python:比较 Python 中的两个具有不同行数和复合键的数据帧 - Python: Compare two dataframes in Python with different number rows and a Compsite key 使用Python中的Pandas索引和匹配两个不同数据框之间的行 - Indexing and matching rows between two different dataframes using Pandas in Python Python: Pandas 比较两个数据帧并获得不同的行 - Python: Pandas compare two dataframes and get the different rows 比较两个不同的pandas数据框并删除行Python - Compare two different pandas dataframes and drop rows Python python:如何添加两个具有不同行数的数据帧 - python: how do I add two dataframes with different number of rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM