简体   繁体   English

如何使用 if & else 组合 pandas 中的三个文本列

[英]How do I combine three text column in pandas using if & else

I'm using python 3.7 I have a pandas data frame with three text columns,name,email & section.我正在使用 python 3.7 我有一个 pandas 数据框,其中包含三个文本列、名称、email 和部分。 The sample data looks like样本数据看起来像

Name   Email              Section
abc    abc@gmail.com      purchase
cde      -                drawing
lmn-pqr      None               -

Hyphen are there in between two words in all of the three columns.在所有三列中的两个单词之间都有连字符。 I would like to join three columns with "_" as separator and create a new column group ignoring None or -.我想用“_”作为分隔符加入三列,并创建一个忽略无或 - 的新列组。 My combined outcome will look like我的综合结果看起来像

Name   Email              Section   Group
abc    abc@gmail.com      purchase  abc_abc@gmail.com_purchase
cde      -                drawing   cde_drawing
lmn-pqr      None               -   lmn-pqr

I'm not sure about the python code.我不确定 python 代码。 Can you please help me?你能帮我么?

You can use str.cat that gets rid of null values:您可以使用str.cat去除 null 值:

df.mask(df.isin(['-', None])).apply(lambda r: r.str.cat(sep='_'), axis=1)

or, manually:或者,手动:

df['Group'] = df.apply(lambda r: '_'.join([x for x in r.replace('-', pd.NA).dropna()]),
                       axis=1)

output: output:

      Name          Email   Section                       Group
0      abc  abc@gmail.com  purchase  abc_abc@gmail.com_purchase
1      cde              -   drawing                 cde_drawing
2  lmn-pqr           None         -                     lmn-pqr

You can try replace - with None then filter it out when join您可以尝试将-替换为None然后在加入时将其过滤掉

df['Group'] = df.replace({'-': None}).apply(lambda row: '_'.join(filter(None, row)), axis=1)
print(df)

      Name          Email   Section                       Group
0      abc  abc@gmail.com  purchase  abc_abc@gmail.com_purchase
1      cde              -   drawing                 cde_drawing
2  lmn-pqr           None         -                     lmn-pqr
df['Group'] = df.apply(lambda x: '-'.join([x['Name'], x['Email'], x['Section']))

x is a Series. x 是一个系列。

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

相关问题 我如何在熊猫中将三列合并为一个时间戳列 - how can i combine three columns to one timestamp column in pandas 如何在具有共享列值的列之间使用 python pandas 准确组合数据? - How do I combine data accurately using python pandas between columns with shared column values? iPython:使用 Pandas,如何组合多个文本文件以查找重复出现的用户名? - iPython:Using Pandas, How do I combine multiple text files to find recurring usernames? 如何绘制使用三列向量的颜色图? - How do I plot a color plot for using three column vectors? 如何组合这两列? 熊猫 - How do I combine these two columns? Pandas pandas:如何将一列中的文本拆分为多行? - pandas: How do I split text in a column into multiple rows? 如何正确删除 Pandas 列中的所有文本? - How do I correctly remove all text from column in Pandas? 在Pandas中,如何检查三个组合的字符串列是否== 10个字符,如果是,则插入新列? - In Pandas, how do I check if three combined string columns == 10 characters, and if so, insert into new column? 在 Python 中使用 Pandas 组合三个数据帧 - Combine Three DataFrames Using Pandas in Python 如何基于一个共同的列,研究站点“名称”组合 2 个熊猫数据框? - How do I combine 2 pandas dataframes based on a common column, research site "Name"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM