Home > Net >  Replace all cells with "-1" in DataFrame
Replace all cells with "-1" in DataFrame

Time:11-17

I have a dataframe like so:

               RANK      COUNT
'2020-01-01'    100         -1
'2020-01-02'     50         -1
'2020-01-03'     -1         75

How can I replace all occurrences of -1 with None and still preserve both the RANK and COUNT as ints?

The result should look like:

               RANK      COUNT
'2020-01-01'    100          
'2020-01-02'     50           
'2020-01-03'                75

If this isn't possible, how can I dump the original data into a .csv file that looks like the desired result?

CodePudding user response:

using replace, replace -1 with ""

out = df.replace(-1, "")
                RANK    COUNT
'2020-01-01'    100     
'2020-01-02'    50  
'2020-01-03'              75

CodePudding user response:

df = df.replace(-1, "")

Second Method

df['RANK'] = df['RANK'].astype(str)
df['COUNT'] = df['COUNT'].astype(str)
df = df.replace('-1', "")
df['RANK'] = df['RANK'].astype(int)
df['COUNT'] = df['COUNT'].astype(int)
  • Related