我有两列值,如下所示:''
column1 Column2
xyz xyzabc
qwe qwerty
''
我想用正向填充方法填充剩余的行。但即使使用了以下代码,它也没有显示任何内容。(当使用df.info时,列没有显示null值,尽管它有((当我使用inplace=True时,它在整列中显示None条目1(
''
df["column1"].fillna( method ='ffill')
df["column2"].fillna(method = 'ffill', inplace = True)
''
首先将空白或空白重新填充为丢失的值:
df = pd.DataFrame({'User': ['acanter ', ' ', ' ', ' ', ' '],
'Name': [' Andy Canter ', ' ', ' ', ' ', ' '],
'Company': [' 135 ', ' 135 ', ' 135 ', ' 000 ', ' 135 '],
'Session': [' ottstpbdeman ', ' ttstptcserver ', ' ottstpjcadaem ', ' ottstpstdlib ', ' bptmmo486m000 '],
'Description': [' ttstpbdeman ', ' Thin Client server ', ' ttstpjcadaemo ', ' stdlib Server ', 'new']})
print (df)
User Name Company Session Description
0 acanter Andy Canter 135 ottstpbdeman ttstpbdeman
1 acanter Andy Canter 135 ttstptcserver Thin Client server
2 acanter Andy Canter 135 ottstpjcadaem ttstpjcadaemo
3 acanter Andy Canter 000 ottstpstdlib stdlib Server
4 acanter Andy Canter 135 bptmmo486m000 new
df = df.replace(r'^s*$', np.nan, regex=True).ffill()
print (df)
User Name Company Session Description
0 acanter Andy Canter 135 ottstpbdeman ttstpbdeman
1 acanter Andy Canter 135 ttstptcserver Thin Client server
2 acanter Andy Canter 135 ottstpjcadaem ttstpjcadaemo
3 acanter Andy Canter 000 ottstpstdlib stdlib Server
4 acanter Andy Canter 135 bptmmo486m000 new