【发布时间】:2020-02-04 02:39:42
【问题描述】:
import pandas as pd
Dates = [list(['None','11/04/1911', '03/06/1919']),
list(['None']),
list(['01/26/1912', '01/15/1918', '02/06/1917']),
list(['None'])]
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 ',
'nothing much'],
'ID': ['E1','E2', 'E3', 'E4'],
'Dates' : Dates,
})
看起来像
Dates ID Text
0 [None, 11/04/1911, 03/06/1919] E1 Hey 10.31.11 22|1|13 03-02-1919 d
1 [None] E2 things here 01-23-18 or 03-23-1984 then
2 [01/26/1912, 01/15/1918, 02/06/1917] E3 stuff 1-22-12 01.11.18 or 2.2.17 so so
3 [None] E4 nothing much
我有以下df。我的目标是替换 ['None'] 例如将 1 和 3 排到一个空列表 [] 。我想要的输出是
Dates ID Text New_Date
0 [None, 11/04/1911, 03/06/1919]
1 []
2 [01/26/1912, 01/15/1918, 02/06/1917]
3 []
我看过Check for None in pandas dataframe 和 Python: most idiomatic way to convert None to empty string? 和 How to replace None only with empty string using pandas?
我也试过了
df['New_Date] = df['Dates].replace('None', list())
如何实现我想要的输出?
【问题讨论】:
标签: python-3.x string pandas list replace