I want to implement operation on excel file in one column that column has string and integer data but that column is object type
My Data looks like in Excel:(Combination of string and number )
Time Spent
3600
0
None
1800
0
I have tried the below code
if (df['Time Spent']=='None').all():
df['Time Spent'] = 0
else:
df['Time Spent'] = df['Time Spent'].astype('int')/3600
Error which I am getting
Index([u'Issue Key', u'Issue Id', u'Summary', u'Assignee', u'Priority',
u'Issue Type', u'Status', u'Tag', u'Original Estimate', u'Time Spent',
u'Resolution Date', u'Created Date'],
dtype='object')
Traceback (most recent call last):
File "dashboard_migration_graph_Resolved.py", line 60, in <module>
df['Time Spent'] = df['Time Spent'].astype('int')/3600
File "/usr/lib64/python2.7/site-packages/pandas/util/_decorators.py", line 118, in wrapper
return func(*args, **kwargs)
File "pandas/_libs/lib.pyx", line 854, in pandas._libs.lib.astype_intsafe
File "pandas/_libs/src/util.pxd", line 91, in util.set_value_at_unsafe
ValueError: invalid literal for long() with base 10: 'None'
Use to_numeric with errors='coerce' for convert all non numbers to missing values, so add Series.fillna before divide:
df['Time Spent'] = pd.to_numeric(df['Time Spent'], errors='coerce').fillna(0)/3600
print (df)
Time Spent
0 1.0
1 0.0
2 0.0
3 0.5
4 0.0
If need None back like misssing value only remove fillna - instead None get missing value NaN, so is possible multiple column:
df['Time Spent'] = pd.to_numeric(df['Time Spent'], errors='coerce')/3600
print (df)
Time Spent
0 1.0
1 0.0
2 NaN
3 0.5
4 0.0
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