0

I have a DataFrame and I need to change the content of the cells of a specific column to a text content (for example "not registered").

I am trying different options, these are some of them:

dftotal.fillna({"Computer_OS":"not registered", "Computer_OS_version":"not registered"}, inplace=True)

dftotal.loc[(dftotal["Computer_OS"]=="NaN"),"Computer_OS"] = "not registered"
  • 1
    Are you using pandas for your dataframe? If so, if would be good to include that information in your question and add the pandas tag. – Alex Waygood Sep 29 '21 at 09:39
  • Does this answer your question? [Replacing column values in a pandas DataFrame](https://stackoverflow.com/questions/23307301/replacing-column-values-in-a-pandas-dataframe) – Tomerikoo Sep 29 '21 at 09:43
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 06 '21 at 21:41

1 Answers1

0

Assumed that all values in Computer_OS column are string datatype else you would need to change datatype first.

import numpy as np
import pandas as pd
import re

def txt2nan(x):
    """
    if given string x contains alphabet 
    return NaN else original x.

    Parameters
    ----------
    x : str
    """
    if re.match('[a-zA-Z]', x):
        return np.nan
    else:
        return x

df = pd.DataFrame({"os":["tsd", "ssad d", "sd", "1","2","3"]})

df["os"] = df["os"].apply(txt2nan)

Better sol'tn is to vectorize above operation:

df["os"] = np.where(df["os"].str.match('[a-zA-Z]'), np.nan, df["os"])
haneulkim
  • 4,406
  • 9
  • 38
  • 80