How to create sets for a column, separated by data types?
Question:
I want to create a set in which only the string values of a column appear. The numerical values of the column are to be stored in another set.
Answers:
Try this:
my_dataset = [
{'id': 1, 'my_column': 'apple', 'value': 10},
{'id': 2, 'my_column': 'banana', 'value': 20},
{'id': 3, 'my_column': 30, 'value': 30},
{'id': 4, 'my_column': 'orange', 'value': 40},
{'id': 5, 'my_column': 50, 'value': 50}
]
string_set = {row['my_column'] for row in my_dataset if isinstance(row['my_column'], str)}
numerical_set = {row['my_column'] for row in my_dataset if isinstance(row['my_column'], (int, float))}
print(string_set)
print(numerical_set)
You can use:
df['numeric'] = pd.to_numeric(df['col'], errors='coerce')
df['non-numeric'] = df['col'].mask(df['numeric'].notna())
Example:
col numeric non-numeric
0 abc NaN abc
1 123 123.0 NaN
2 123 123.0 NaN
3 NaN NaN NaN
Used input:
df = pd.DataFrame({'col': ['abc', 123, '123', float('nan')]})
I want to create a set in which only the string values of a column appear. The numerical values of the column are to be stored in another set.
Try this:
my_dataset = [
{'id': 1, 'my_column': 'apple', 'value': 10},
{'id': 2, 'my_column': 'banana', 'value': 20},
{'id': 3, 'my_column': 30, 'value': 30},
{'id': 4, 'my_column': 'orange', 'value': 40},
{'id': 5, 'my_column': 50, 'value': 50}
]
string_set = {row['my_column'] for row in my_dataset if isinstance(row['my_column'], str)}
numerical_set = {row['my_column'] for row in my_dataset if isinstance(row['my_column'], (int, float))}
print(string_set)
print(numerical_set)
You can use:
df['numeric'] = pd.to_numeric(df['col'], errors='coerce')
df['non-numeric'] = df['col'].mask(df['numeric'].notna())
Example:
col numeric non-numeric
0 abc NaN abc
1 123 123.0 NaN
2 123 123.0 NaN
3 NaN NaN NaN
Used input:
df = pd.DataFrame({'col': ['abc', 123, '123', float('nan')]})