通过将其他两个字符串格式连接起来来创建新列

时间:2019-06-27 12:20:55

标签: python pandas dataframe

我有以下代码将两列连接成一个新列:

import pandas as pd

d = {
    'name': ['Tomek', 'Jacek', 'Placek'],
    'surname': ['A', 'B', 'C'],
}

df = pd.DataFrame(d)
df['fullname'] = df['name'] + ' ' + df['surname']
print(df)

# output:
#    name surname  fullname
# 0   Tomek       A   Tomek A
# 1   Jacek       B   Jacek B
# 2  Placek       C  Placek C

但是,我需要改用字符串格式:

import pandas as pd

d = {
    'name': ['Tomek', 'Jacek', 'Placek'],
    'surname': ['A', 'B', 'C'],
}

df = pd.DataFrame(d)
df['fullname'] = f"{df['name']} {df['surname']}"
print(df)

但是输出是:

#     name surname                                           fullname
# 0   Tomek       A  0     Tomek\n1     Jacek\n2    Placek\nName: n...
# 1   Jacek       B  0     Tomek\n1     Jacek\n2    Placek\nName: n...
# 2  Placek       C  0     Tomek\n1     Jacek\n2    Placek\nName: n...

如何使用字符串格式,并且仍然具有与第一个类似的输出?

2 个答案:

答案 0 :(得分:1)

  

我需要改用字符串格式

使用zip(),然后列出理解力

df['fill_name']=[f"{a} {b}" for a,b in zip(df.name,df.surname)]
print(df)

     name surname fill_name
0   Tomek       A   Tomek A
1   Jacek       B   Jacek B
2  Placek       C  Placek C

答案 1 :(得分:1)

无需列表理解即可尝试:

更多详细信息cat() of the .str accessor

import pandas as pd

d = {
    'name': ['Tomek', 'Jacek', 'Placek'],
    'surname': ['A', 'B', 'C'],
}

df = pd.DataFrame(d)
df['fill_name']= df.name.str.cat(df.surname,sep=" ")
print(df)

O / P:

     name surname fill_name
0   Tomek       A   Tomek A
1   Jacek       B   Jacek B
2  Placek       C  Placek C