pandas-3
pandas-3
origin name
0 1 chevrolet chevelle malibu
1 1 buick skylark 320
2 1 plymouth satellite
3 1 amc rebel sst
4 1 ford torino
[3]: int(True)
[3]: 1
[4]: float(1)
[4]: 1.0
[5]: complex(1.0)
[5]: (1+0j)
[6]: str(1+0j)
[6]: '(1+0j)'
[ ]: # bool --> int --> float --> complex --> strings ( strings is at highest level)
1
[8]: pd.Series([1,2,3,4,5,1.4]) # as Series can take one single data type , it will␣
↪assign data type based on hierarchy
[8]: 0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
5 1.4
dtype: float64
[ ]: # Null values : 1. machine error machine was not able to capture this␣
↪information
[19]: int(2)*2
[19]: 4
[20]: int('2') *2
[20]: 4
[21]: '22'
[9]: 0 1
1 2
2 3
3 4
4 5
5 abc
dtype: object
[11]: 1*2
[11]: 2
[16]: int('1') * 2
[16]: 2
2
[22]: 'aa'
[23]: '2' *2
[23]: '22'
[ ]: import os
os.getcwd() # get current working directory
[ ]: a function which was created using def block can be used infinite times across␣
↪the python code
if you know the function that you need to use is not used more than once -->␣
↪lambda ( if function logic is simple )
[25]: df1.head()
[ ]: inplace = True --> changes will be committed to df1 not to your excel file
[27]: pwd
[27]: 'C:\\Users\\admin\\2802'
3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[17], line 1
----> 1 int('abc')
[18]: help(pd.to_numeric)
Please note that precision loss may occur if really large numbers
are passed in. Due to the internal limitations of `ndarray`, if
numbers smaller than `-9223372036854775808` (np.iinfo(np.int64).min)
or larger than `18446744073709551615` (np.iinfo(np.uint64).max) are
passed in, it is very likely they will be converted to float so that
they can be stored in an `ndarray`. These warnings apply similarly to
`Series` since it internally leverages `ndarray`.
Parameters
----------
arg : scalar, list, tuple, 1-d array, or Series
Argument to be converted.
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
- If 'raise', then invalid parsing will raise an exception.
- If 'coerce', then invalid parsing will be set as NaN.
- If 'ignore', then invalid parsing will return the input.
.. versionchanged:: 2.2
4
possible according to the following rules:
.. versionadded:: 2.0
Returns
-------
ret
Numeric if parsing succeeded.
Return type depends on input. Series if Series, otherwise ndarray.
See Also
--------
DataFrame.astype : Cast argument to a specified dtype.
to_datetime : Convert argument to datetime.
to_timedelta : Convert argument to timedelta.
numpy.ndarray.astype : Cast a numpy array to a specified type.
DataFrame.convert_dtypes : Convert dtypes.
Examples
--------
Take separate series and convert to numeric, coercing when told to
5
2 -3.0
dtype: float64
>>> pd.to_numeric(s, downcast='float')
0 1.0
1 2.0
2 -3.0
dtype: float32
>>> pd.to_numeric(s, downcast='signed')
0 1
1 2
2 -3
dtype: int8
>>> s = pd.Series(['apple', '1.0', '2', -3])
>>> pd.to_numeric(s, errors='coerce')
0 NaN
1 1.0
2 2.0
3 -3.0
dtype: float64
[31]: 0 Even
1 Odd
2 Odd
3 Even
4 Odd
…
6
392 Odd
393 Odd
394 Odd
395 Odd
396 Odd
Name: acceleration, Length: 397, dtype: object
[33]: df1.head(2)
[ ]: ['col1','col2']
7
.. … … …
12 1 chevrolet monte carlo Odd
6 1 chevrolet impala Odd
7 1 plymouth fury iii Odd
9 1 amc ambassador dpl Odd
11 1 plymouth 'cuda 340 Even
[38]: df1['mpg']
[38]: 0 18.0
1 15.0
2 NaN
3 NaN
4 NaN
…
392 27.0
393 44.0
394 32.0
395 28.0
396 31.0
Name: mpg, Length: 397, dtype: float64
8
394 32.0 2295 11.6
395 28.0 2625 18.6
396 31.0 2720 19.4
9
79 2 renault 12 (sw) Even
80 1 ford pinto (sw) Even
101 1 plymouth duster Even
113 1 mercury capri v6 Even
122 2 saab 99le Even
148 2 fiat 124 tc Even
151 2 fiat x1.9 Even
167 3 toyota corolla Even
175 2 volkswagen rabbit Even
234 1 pontiac sunbird coupe Even
293 2 vw rabbit custom Even
305 1 buick skylark limited Even
331 3 subaru dl Even
349 3 mazda glc 4 Even
369 1 chevrolet cavalier 2-door Even
371 1 dodge aries se Even
372 1 pontiac phoenix Even
[42]: df1[(df1['mpg'] > 20) & (df1['col1'] == 'Even')] # it will return the rows␣
↪where cond1 is set to True
10
17 1 ford maverick Even
31 3 toyota corona Even
49 1 mercury capri 2000 Even
50 2 opel 1900 Even
54 3 datsun 1200 Even
77 2 volkswagen 411 (sw) Even
79 2 renault 12 (sw) Even
80 1 ford pinto (sw) Even
101 1 plymouth duster Even
113 1 mercury capri v6 Even
122 2 saab 99le Even
148 2 fiat 124 tc Even
151 2 fiat x1.9 Even
167 3 toyota corolla Even
175 2 volkswagen rabbit Even
234 1 pontiac sunbird coupe Even
293 2 vw rabbit custom Even
305 1 buick skylark limited Even
331 3 subaru dl Even
349 3 mazda glc 4 Even
369 1 chevrolet cavalier 2-door Even
371 1 dodge aries se Even
372 1 pontiac phoenix Even
[43]: df1[pd.isna(df1['mpg'])]
11
8 14.0 8.0 455.0 225 4425 10.0 70
9 15.0 8.0 390.0 190 3850 8.5 70
.. … … … … … … …
392 27.0 4.0 140.0 86 2790 15.6 82
393 44.0 4.0 97.0 52 2130 24.6 82
394 32.0 4.0 135.0 84 2295 11.6 82
395 28.0 4.0 120.0 79 2625 18.6 82
396 31.0 4.0 119.0 82 2720 19.4 82
12