Home > Software engineering >  string 'None' and NoneType in python's dataframe
string 'None' and NoneType in python's dataframe

Time:02-06

Make a list containing two elements : 'None' and None.

x = ['None',None]
x
['None', None]
print(x)
['None', None]

It is obvious that the first is a string whose value is 'None', the second is a NoneType None in python.

Create a dataframe which contain the two element also:

df = pd.DataFrame({"content": ['None', None]})
type(df.iloc[0,0] )
<class 'str'>
type(df.iloc[1,0])
<class 'NoneType'>
df
  content
0    None
1    None
print(df)
  content
0    None
1    None

Maybe python community should make a progress to display it such as shown the list:

df
  content
0    'None'
1    None
print(df)
  content
0    'None'
1    None

CodePudding user response:

First of all, in normal cases, there should not be two types of data in one column of a completed DataFrame. Therefore a str and a NoneType cannot show themselves together after data washing.

And in normal cases, we don't want to see so many '' in a string column. It will be annoying. And if you do want to see them, try:

df.content.apply(lambda x: x if not isinstance(x,str) else repr(x))

And the result is what you want:

0    'None' 
1      None  
Name: content, dtype: object
  •  Tags:  
  • Related