我有以下示例df
import pandas as pd
DateChange = [list(['11/04/1911', '03/06/1919']),
list(['01/27/1918', '03/27/1984']),
list(['01/26/1912', '01/15/1918', '02/06/1917'])]
DateGrab = [list(['10.31.11', '03-02-1919']), list(['01-23-18', '03-23-1984']),
list(['1-22-12', '01.11.18', '2.2.17'])]
df= pd.DataFrame({'Text':['Hey 10.31.11 22|1|13 03-02-1919 d',
'things here 01-23-18 or 03-23-1984 then ',
'stuff 1-22-12 01.11.18 or 2.2.17 so so '],
'ID': ['E1','E2', 'E3'],
'DateChange' : DateChange,
'DateGrab' : DateGrab
})
看起来像
DateChange DateGrab ID Text
0 [11/04/1911, 03/06/1919] [10.31.11, 03-02-1919] E1 Hey 10.31.11 22|1|13 03-02-1919 d
1 [01/27/1918, 03/27/1984] [01-23-18, 03-23-1984] E2 things here 01-23-18 or 03-23-1984 then
2 [01/26/1912, 01/15/1918, 02/06/1917] [1-22-12, 01.11.18, 2.2.17]E3 stuff 1-22-12 01.11.18 or 2.2.17 so so
我的目标如下:
1)如果DateGrab
中存在10.31.11
值,例如Text
,则将Text
中的该值替换为DateChange
,例如。 11/04/1911
2)创建新列
因此,我想要的输出如下
DateChange DateGrab ID Text New_Text
0 Hey 11/04/1911 22|1|13 03/06/1919 d
1 things here 01/27/1918 or 03/27/1984 then
2 stuff 01/26/1912 01/15/1918 or 02/06/1917 so so
我已经尝试过以下代码,但这不起作用
df['New_Text']= df['Text'].replace(df.Text, df.DateChange)
如何实现所需的输出?
答案 0 :(得分:1)
您可以在熊猫0.25.1中使用explode:
s=df['DateChange'].explode()
s.index=df['DateGrab'].explode()
d=s.to_dict()
for key in d:
df['New_Text']=df['Text'].str.replace(key,d[key])
print(df)
Text ID \
0 Hey 10.31.11 22|1|13 03-02-1919 d E1
1 things here 01-23-18 or 03-23-1984 then E2
2 stuff 1-22-12 01.11.18 or 2.2.17 so so E3
DateChange DateGrab \
0 [11/04/1911, 03/06/1919] [10.31.11, 03-02-1919]
1 [01/27/1918, 03/27/1984] [01-23-18, 03-23-1984]
2 [01/26/1912, 01/15/1918, 02/06/1917] [1-22-12, 01.11.18, 2.2.17]
New_Text
0 Hey 10.31.11 22|1|13 03-02-1919 d
1 things here 01-23-18 or 03-23-1984 then
2 stuff 1-22-12 01.11.18 or 02/06/1917 so so
答案 1 :(得分:1)
尝试利用字典中的switch menuType {
case .about:
let profileVC = AboutViewController()
self.present(profileVC, animated: true, completion: nil)
case .imageList:
let imageVC = ImageViewController()
self.present(imageVC, animated: true, completion: nil)
case default:
break
}
df.replace