pandas.DataFrame set all string values to nan


Fahawa

I have a pandas.DataFrametype that contains strings, floats and ints.

Is there a way to set all strings that cannot be converted to floats to NaN?

E.g:

    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8  "wajdi"

to:

    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8    NaN
rwhitt2049

You can use pd.to_numericand seterrors='coerce'

pandas.to_numeric

df['D'] = pd.to_numeric(df.D, errors='coerce')

This will give you:

    A   B   C   D
0   1   2   5.0 7.0
1   0   4   NaN 15.0
2   4   8   9.0 10.0
3   11  5   8.0 0.0
4   11  5   8.0 NaN

Deprecated solution (pandas only <= 0.20):

df.convert_objects(convert_numeric=True)

pandas.DataFrame.convert_objects

Here are the developer notes in the convert_objectssource code : # TODO: Remove in 0.18 or 2017, which ever is sooner. So if you use it, don't use it as a long-term solution.

Related


pandas.DataFrame set all string values to nan

Fahawa I have a pandas.DataFrametype that contains strings, floats and ints. Is there a way to set all strings that cannot be converted to floats to NaN? E.g: A B C D 0 1 2 5 7 1 0 4 NaN 15 2 4 8 9 10 3 11 5 8 0 4

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

pandas.DataFrame set all string values to nan

Fahawa I have a pandas.DataFrametype that contains strings, floats and ints. Is there a way to set all strings that cannot be converted to floats to NaN? E.g: A B C D 0 1 2 5 7 1 0 4 NaN 15 2 4 8 9 10 3 11 5 8 0 4

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

All NAN values when filtering Pandas DataFrame to one column

Taylor I am importing data from a .csv file which is stored in a dataframe. It looks good there: After that I try to store only one column of the dataframe elsewhere. However, it returns all NaN values: The exact same code works for the previous .xls file in t

pandas.DataFrame set all string values to nan

Fahawa I have one pandas.DataFramethat contains string, float and integer types. Is there a way to set all strings that cannot be converted to floats to NaN? E.g: A B C D 0 1 2 5 7 1 0 4 NaN 15 2 4 8 9 10 3 11 5 8

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

pandas.DataFrame set all string values to nan

Fahawa I have a pandas.DataFrametype that contains strings, floats and ints. Is there a way to set all strings that cannot be converted to floats to NaN? E.g: A B C D 0 1 2 5 7 1 0 4 NaN 15 2 4 8 9 10 3 11 5 8 0 4

pandas.DataFrame set all string values to nan

Fahawa I have a pandas.DataFrametype that contains strings, floats and ints. Is there a way to set all strings that cannot be converted to floats to NaN? E.g: A B C D 0 1 2 5 7 1 0 4 NaN 15 2 4 8 9 10 3 11 5 8 0 4

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get