python 在Pandas中加入多个列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在Pandas中加入多个列相关的知识,希望对你有一定的参考价值。

# In the original dataframe 6 columns were present with similar names, containing strings of data
# For 1 row on one of the 6 columns contained information, the other 5 was nan, so a joining had to be made to clean up data

# Find all the column names which contain a specific string
# The 6 similar column names all contained the string "ofeed"
cofeed_cols = [col for col in df_rec.columns if "ofeed" in col]
cofeed_cols = [str(z) for z in cofeed_cols]

# Joining the content of multiple cofeed columns in one, called Cofeed all, nan values are ignored with dropna and all values are string
df_rec["Cofeed all"] = df_rec[cofeed_cols].apply(lambda x: "".join(x.dropna().astype(str)), axis=1)

以上是关于python 在Pandas中加入多个列的主要内容,如果未能解决你的问题,请参考以下文章