'How to check & change datatype of a column of panda dataframe using combobox in jupyter notebook?
I would like to convert the data type of a column but using ipython wigets:
- Combobox1 will display all columns of the data frame.
- Disabled Text1 box will show the existing datatype of the column selected at step 1.
- Next Combobox2 will give options like, {'int', 'float', 'datetime', 'object'}
- Text2 box will show something like: df['customerID'].dtypes = String
- Output in Text2 must be stored in a variable for future programming.
- Button to execute data type conversion.
Code that I referred(but not been able to customize completely):
# Common Imports for Widgets
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
df = pd.read_csv('TelcomCustomer-Churn.csv', index_col = 0)
foo = df.columns
# Function to apply to drop box object
def bar(x):
print(x,'\n') # Whole object
print(x.new,'\n') # New value
# creation of a widget dropdown object
box1 = widgets.Dropdown(
options=foo, # Object to iterate over
description='Column Name:',
value=foo[1], # Default value selection
rows=len(foo), # The number of rows to display when showing the box
interactive=True,
);
#text box
text = widgets.Text(
value='last',
placeholder='datatype',
description='String:',
disabled=True
)
def callback(wdgt):
with output:
clear_output
display(wdgt.value)
text.on_submit(callback)
# Drop box of k,v like pairs
box2 = widgets.Dropdown(
options=[('Object', 1), ('float', 2), ('int', 3), ('datetime', 4)],
value=1,
description='datatypes:',
)
# Button to click
select_button = widgets.Button(
description='Convert Datatype',
# missing action to be perfomed
disabled=False
)
# Event Handlers
box1.observe(bar,names='value')
text.observe(bar,names='value')
box2.observe(bar,names='value')
select_button.on_click(get_user_selection)
[enter image description here][1]
ui = widgets.HBox([box1,text,box2,select_button])
# display the UI
display(ui)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
