我有一个包含列的输入数据框:
Template Template Name
This is String
This is String line
This is Int
This is Int Name
This is String Name
String Name is none
Int is empty
预期输出数据帧:
Template Template Name
This is String String
This is String line String
This is Int Int
This is Int Name Int
This is String Name String
String Name is none String
Int is empty Int
我尝试过以下代码
all_data['Template Name'] = all_data['Template'].str.contains('String')
if all_data['Template'].str.contains('String').any() == True:
all_data['Template Name'] = 'String'
但它只是打印' String'在所有细胞中,请帮助我。
答案 0 :(得分:2)
我认为你需要extract
:
df['Template Name'] = df.Template.str.extract('(String|Int)', expand=False)
print (df)
Template Template Name
0 This is String String
1 This is String line String
2 This is Int Int
3 This is Int Name Int
4 This is String Name String
5 String Name is none String
6 Int is empty Int