I need to select columns in Pandas which contain only numeric values in column names, for example:
df=
0 1 2 3 4 window_label next_states ids
0 17.0 18.0 16.0 15.0 15.0 ddddd d 13.0
1 18.0 16.0 15.0 15.0 16.0 ddddd d 13.0
2 16.0 15.0 15.0 16.0 15.0 ddddd d 13.0
3 15.0 15.0 16.0 15.0 17.0 ddddd d 13.0
4 15.0 16.0 15.0 17.0 NaN ddddd d 13.0
so I need to select only first five columns. Something like:
df[df.columns.isnumeric()]
EDIT
I came up with the solution:
digit_column_names = [num for num in list(df.columns) if isinstance(num, (int,float))]
df_new = df[digit_column_names]
not very pythonic or pandasian, but it works.
Try
df.ids = df.ids.astype('object')
new_df = df.select_dtypes([np.number])
0 1 2 3 4
0 17.0 18.0 16.0 15.0 15.0
1 18.0 16.0 15.0 15.0 16.0
2 16.0 15.0 15.0 16.0 15.0
3 15.0 15.0 16.0 15.0 17.0
4 15.0 16.0 15.0 17.0 NaN
EDIT: If you are interested in selecting column names that are numeric, here is something that you can do.
df = pd.DataFrame({0: [1,2], '1': [3,4], 'blah': [5,6], 2: [7,8]})
df.columns = pd.to_numeric(df.columns, errors = 'coerce')
df[df.columns.dropna()]
You get
0.0 1.0 2.0
0 1 3 7
1 2 4 8
How about this solution?
This checks if every character of the column is a digit.
cols = [col for col in df.columns if all(char.isdigit() for char in col)]
df[cols]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With