简体   繁体   English

如何合并 pandas dataframe 中具有相似名称的列?

[英]How do I merge columns that have similar names in a pandas dataframe?

I have a dataframe which has the words Due Date written differently but it all means the same.我有一个 dataframe,它的截止日期写法不同,但意思相同。 The problem is in my master data(xls file), one due date has an extra space or doesnt and i cant change that.All i can change is my final output.问题出在我的主数据(xls 文件)中,一个截止日期有额外的空间或没有,我无法更改它。我只能更改我的最终 output。

Sr no Due Date    Due Date   DueDate
1     1/2/22      
2                  1/5/22    
3
4                         
5                             ASAP

I just want that column 2 and 3 all combine under column one at the same location they were我只希望第 2 列和第 3 列全部合并到第 1 列下的相同位置

Sr No.  Due Date
1        1/2/22
2        1/5/22
3        
4
5        ASAP

Try with bfill尝试使用bfill

out = df.bfill(axis = 1)[['Sr No','Due Date']]

You can use filter with a regex to get similar names, then bfill and get the first.您可以使用带有正则表达式的filter来获取相似的名称,然后bfill并获取第一个。 Finally join to original devoid of the found columns:最后加入没有找到的列的原始文件:

d = df.filter(regex=r'(?i)due\s*date')
df2 = (df
 .drop(columns=list(d.columns))
 .join(d.bfill(1).iloc[:,0])
 )

Output: Output:

   Sr no Due Date
0      1   1/2/22
1      2   1/5/22
2      3     None
3      4     None
4      5     ASAP

Possible solution is the following:可能的解决方案如下:

import pandas as pd

# set test data
data = {"Sr no": [1,2,3,4,5],
        "Due Date": ["1/2/22", "", "", "", ""], 
        "Due Date ": ["", "1/2/22", "", "", ""],
        " Due Date": ["", "", "", "", "ASAP"]
       }

# create pandas dataframe
df = pd.DataFrame(data)

在此处输入图像描述

# clean up column names 
df.columns = [col.strip() for col in df.columns]

# group data
df = df.groupby(df.columns, axis=1).agg(lambda x: x.apply(lambda y: ''.join([str(l) for l in y if str(l) != "nan"]), axis=1))

# reorder column
df = df[['Sr no', 'Due Date']]

df

Returns退货

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不丢失数据的情况下合并 Pandas Dataframe 中具有相似名称的多个列 - How do I merge multiple columns with similar names in a Pandas Dataframe without losing data 如何将具有相似名称的列的 pandas dataframe 转换为行? - How can I turn a pandas dataframe with columns with similar names into rows? Pandas:合并具有相似名称的列 - Pandas: merge columns with the similar names 熊猫如何将相似的列名称合并在一起? - pandas how do i merge similar columns name together? 熊猫-如何拆分和合并名称相似的列? - Pandas- how to split and merge columns with similar names? 如何合并多个熊猫数据框列 - How do I merge multiple pandas dataframe columns 如何使用 pandas 返回 dataframe 中名称相似的列 - How to return columns with with similar names in a dataframe using pandas 给定一个数据框,如何根据名称对列进行存储并将同一存储桶中的列合并为一? - Given a dataframe, how do I bucket columns according to their names and merge columns in the same bucket into one? 如何在具有相似值(但不相同)的公共列上合并 Pandas 中的两个数据框? - How do I merge two data frames in pandas on a common column which have similar values (but not the same)? 如何合并两个数据帧,一列是另一个数据帧中所有列的名称? - How do I merge two dataframes, one column are the names of all the columns in the other dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM