我有dataframe
像波纹管
Data1 Data2
Andy_23_1985 0.78
John_24_1985 0.78
您知道我如何使用Python提取第二个单词吗?
Data1 Data2 Age
Andy_23_1985 0.78 23
John_24_1985 0.78 24
答案 0 :(得分:1)
您可以将str.split()
与expand=True
一起使用:
df = pd.DataFrame({'Data1':['Andy_23_1985', 'John_24_1985'], 'Data2':[0.78, 0.78]})
df['Age'] = df['Data1'].str.split('_', expand=True)[1]
print(df)
打印:
Data1 Data2 Age
0 Andy_23_1985 0.78 23
1 John_24_1985 0.78 24
答案 1 :(得分:0)
尝试一下:
df = pd.read_clipboard() # I copy/pasted you dataframe
df['Age'] = df.Data1.str.extract(r'(\d+)')
or more precise if you don't want the first match:
df['Age'] = df.Data1.str.extract(r'_(\d\d)_')
Data1 Data2 Age
0 Andy_23_1985 0.78 23
1 John_24_1985 0.78 24