Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Created November 28, 2021 06:01
Show Gist options
  • Save jj-github-jj/d9612c97aa0b7b701b3a07acefd4251d to your computer and use it in GitHub Desktop.
Save jj-github-jj/d9612c97aa0b7b701b3a07acefd4251d to your computer and use it in GitHub Desktop.
# convert data frame strings to numeric where possible and drop rows with non numeric content
# convert data frame strings to numeric where possible and drop rows with non numeric content
df23=df23.apply(pd.to_numeric, errors='ignore') # converts to numbers when possible for all columns
df23
c=df23.columns.to_list()
data_columns=[c[x] for x in range(1,6)]
# Eliminate invalid data from dataframe (see Example below for more context)
num_df = (df23.drop(data_columns, axis=1)
.join(df23[data_columns].apply(pd.to_numeric, errors='coerce'))) #numeric dataframe
num_df = num_df[num_df[data_columns].notnull().all(axis=1)]
num_df
#-----------------------
# remove blanks from column names in dataframe since some tools dont accept blanks in strings
df.columns = df.columns.str.replace(' ', '_')
df.rename(columns={'two':'new_name'}, inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment