Import As
Import As
Import As
In [6]: a=np.arange(6)
a
In [7]: a.reshape(2,3)
[3, 4, 5]])
In [8]: a.any()
Out[8]: True
In [9]: a.all()
Out[9]: False
In [10]: a.mean()
Out[10]: 2.5
In [11]: a.argmin()
Out[11]: 0
In [12]: a.argmin()
Out[12]: 0
In [13]: a.cumsum()
In [14]: a.sum()
Out[14]: 15
In [15]: a.cumprod()
In [ ]:
In [ ]:
In [16]: np.linspace(-5,5,100)
vedio
panda series
In [17]: np.zeros(((2,2,2)))
[0., 0.]],
[[0., 0.],
[0., 0.]]])
In [18]: np.zeros(2)
In [19]: np.ones((2,3))
In [20]: np.empty((1,2))
In [22]: np.diag([1,2,3,4])
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
In [23]: b=np.arange(15)
b
In [24]: c=b.reshape(3,5)
c
[ 5, 6, 7, 8, 9],
In [25]: c[0:2,:]
[5, 6, 7, 8, 9]])
In [26]: c[2,1:3]
In [27]: c[0:3,1::2]
[ 6, 8],
[11, 13]])
In [28]: x=np.arange(9)
x
In [29]: y=x.reshape(3,3)
y
[3, 4, 5],
[6, 7, 8]])
In [30]: z=y+y
z
[ 6, 8, 10],
In [31]: z.sum()
Out[31]: 72
In [32]: z.sum(axis=0)
In [33]: z.sum(axis=1)
In [34]: print(np.char.strip(['anita','babaad'],'a'))
['nit' 'babaad']
In [35]: a=np.arange(9)
a
In [36]: np.split(a,3)
In [37]: np.split(a,[3,5,7])
In [38]: a =np.arange(1,7)
a
In [39]: a.reshape(3,2)
[3, 4],
[5, 6]])
In [40]: np.resize(a,(2,3))
[4, 5, 6]])
In [41]: import matplotlib.pyplot as plt
C:\Users\HP\AppData\Local\Temp\ipykernel_9568\3214265231.py:3: RuntimeWarning:
divide by zero encountered in log10
b= np.log10(a)
In [45]: x= np.arange(0,3*np.pi,0.1)
y=np.sin(x)
plt.plot(x,y)
In [46]: x=np.linspace(-5,5,10)
x
Out[47]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8,
3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1,
5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4,
6.5, 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7,
7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9. ,
In [48]: x= np.linspace(0,3*np.pi,90)
y=np.sin(x)
plt.plot(x,y)
[0 1 2 3 4 5]
[[0 1 2]
[3 4 5]]
[3 5 7]
[ 3 12]
15
In [52]: print(np.sqrt(a))
print(np.std(a))
1.707825127659933
In [53]: print(a.ravel())
[0 1 2 3 4 5]
In [54]: a=np.zeros((6,6),dtype=complex)
a
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0]])
Out[57]: 0 1
1 2
2 3
3 4
4 5
dtype: int64
Out[58]: 0 1
1 2
2 3
3 4
dtype: int64
In [59]: a = np.random.randn(5)
a
index = ['a','b','c','d','e']
b=pd.Series(a,index=index)
b
Out[59]: a -0.089431
b 0.654129
c 1.414282
d 0.307507
e 0.500292
dtype: float64
In [60]: d={'a':1,'b':2,'c':3}
d
In [61]: a = pd.Series(d)
a
Out[61]: a 1
b 2
c 3
dtype: int64
Out[62]: A 1
B 2
C 3
dtype: int64
In [63]: a[1:3]
Out[63]: B 2
C 3
dtype: int64
In [64]: a[:2]
Out[64]: A 1
B 2
dtype: int64
In [65]: x=s.append(a)
x
C:\Users\HP\AppData\Local\Temp\ipykernel_9568\1146264426.py:1: FutureWarning: T
he series.append method is deprecated and will be removed from pandas in a futu
re version. Use pandas.concat instead.
x=s.append(a)
Out[65]: 0 1
1 2
2 3
3 4
A 1
B 2
C 3
dtype: int64
In [66]: x.drop('b')
---------------------------------------------------------------------------
----> 1 x.drop('b')
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_d
ecorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.
wrapper(*args, **kwargs)
326 warnings.warn(
327 msg.format(arguments=_format_argument_list(allow_args)),
328 FutureWarning,
329 stacklevel=find_stack_level(),
330 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\se
ries.py:5237, in Series.drop(self, labels, axis, index, columns, level, inplac
e, errors)
5142 self,
(...)
5151 """
5153
(...)
5236 """
5238 labels=labels,
5239 axis=axis,
5240 index=index,
5241 columns=columns,
5242 level=level,
5243 inplace=inplace,
5244 errors=errors,
5245 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_d
ecorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.
wrapper(*args, **kwargs)
326 warnings.warn(
327 msg.format(arguments=_format_argument_list(allow_args)),
328 FutureWarning,
329 stacklevel=find_stack_level(),
330 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ge
neric.py:4505, in NDFrame.drop(self, labels, axis, index, columns, level, inpla
ce, errors)
4507 if inplace:
4508 self._update_inplace(obj)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ge
neric.py:4546, in NDFrame._drop_axis(self, labels, axis, level, errors, only_sl
ice)
4545 else:
4550 else:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\in
dexes\base.py:6975, in Index.drop(self, labels, errors)
6973 if mask.any():
In [ ]: a1.add(b1)
In [ ]: a.sub(b)
In [ ]: a.mul(b)
In [ ]: a.div(b)
In [ ]: print(a.median())
print(a.max())
print(a.min())
In [67]: dates = pd.date_range('today',periods = 6)
dates
dtype='datetime64[ns]', freq='D')
Out[68]:
A B C D
In [69]: n=np.arange(5)
n
In [70]: m=pd.DataFrame(n)
m
Out[70]:
0
0 0
1 1
2 2
3 3
4 4
In [71]: data = {'animals':['cat','dog','cat','dog','snake'],
'age':[2,2.5,np.nan,4.5,3],
'visits':[1,2,3,2,1]}
labels= ['a','b','c','d','e']
df = pd.DataFrame(data, index = labels)
df
Out[71]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [72]: df.dtypes
age float64
visits int64
dtype: object
In [73]: df.head()
Out[73]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [74]: df.tail(4)
Out[74]:
animals age visits
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [75]: df['animals']
Out[75]: a cat
b dog
c cat
d dog
e snake
In [ ]:
In [ ]:
In [76]: df.columns
In [77]: df.values
Out[78]:
age visits
Out[79]:
animals age visits
a cat 2.0 1
b dog 2.5 2
e snake 3.0 1
d dog 4.5 2
c cat NaN 3
In [80]: df[1:3]
Out[80]:
animals age visits
b dog 2.5 2
c cat NaN 3
In [81]: df.sort_values(by='age')[1:3]
Out[81]:
animals age visits
b dog 2.5 2
e snake 3.0 1
In [82]: df[['age','visits']]
Out[82]:
age visits
a 2.0 1
b 2.5 2
c NaN 3
d 4.5 2
e 3.0 1
In [83]: df[2:4]
Out[83]:
animals age visits
c cat NaN 3
d dog 4.5 2
In [84]: df[2:3]
Out[84]:
animals age visits
c cat NaN 3
In [85]: df1=df.copy()
df1
Out[85]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [86]: df1.isnull()
Out[86]:
animals age visits
In [88]: df1
Out[88]:
animals age visits (d, age)
C:\Users\HP\AppData\Local\Temp\ipykernel_9568\2053335143.py:1: FutureWarning: T
he default value of numeric_only in DataFrame.mean is deprecated. In a future v
ersion, it will default to False. In addition, specifying 'numeric_only=None' i
s deprecated. Select only valid columns or specify the value of numeric_only to
silence this warning.
df1.mean()
visits 1.8
dtype: float64
In [90]: df1['visits'].sum()
Out[90]: 9
In [91]: df.sum()
age 12.0
visits 9
dtype: object
Out[92]: 0 A
1 B
2 C
3 D
dtype: object
Out[93]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [94]: df3 = df2.copy()
df3
Out[94]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [95]: df3['age'].mean()
df3.fillna(4)
Out[95]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat 4.0 3
d dog 4.5 2
e snake 3.0 1
Out[96]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat 3.0 3
d dog 4.5 2
e snake 3.0 1
In [97]: df4 = df3.copy()
df4
Out[97]:
animals age visits
a cat 2.0 1
b dog 2.5 2
c cat NaN 3
d dog 4.5 2
e snake 3.0 1
In [98]: df4.dropna(how="any")
Out[98]:
animals age visits
a cat 2.0 1
b dog 2.5 2
d dog 4.5 2
e snake 3.0 1
In [99]: df4.to_csv('animal.csv')
Out[100]:
Unnamed: 0 animals age visits
0 a cat 2.0 1
1 b dog 2.5 2
2 c cat NaN 3
In [101]: df4.to_excel('animal.xlsx',)
---------------------------------------------------------------------------
----> 1 df4.to_excel('animal.xlsx',)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_d
ecorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper
(*args, **kwargs)
209 else:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_d
ecorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper
(*args, **kwargs)
209 else:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ge
neric.py:2374, in NDFrame.to_excel(self, excel_writer, sheet_name, na_rep, floa
t_format, columns, header, index, index_label, startrow, startcol, engine, merg
e_cells, encoding, inf_rep, verbose, freeze_panes, storage_options)
2364 df,
2365 na_rep=na_rep,
(...)
2372 inf_rep=inf_rep,
2373 )
2375 excel_writer,
2376 sheet_name=sheet_name,
2377 startrow=startrow,
2378 startcol=startcol,
2379 freeze_panes=freeze_panes,
2380 engine=engine,
2381 storage_options=storage_options,
2382 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\form
ats\excel.py:910, in ExcelFormatter.write(self, writer, sheet_name, startrow, s
tartcol, freeze_panes, engine, storage_options)
907 else:
912 )
915 try:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\exce
l\_openpyxl.py:56, in OpenpyxlWriter.__init__(self, path, engine, date_format,
datetime_format, mode, storage_options, if_sheet_exists, engine_kwargs, **kwar
gs)
43 def __init__(
44 self,
(...)
54 ) -> None:
60 super().__init__(
61 path,
62 mode=mode,
(...)
65 engine_kwargs=engine_kwargs,
66 )
In [102]: print(np.char.center('hello',20,fillchar='-'))
-------hello--------
In [103]: a=np.arange(9)
a
In [104]: a[5]
Out[104]: 5
In [105]: b=slice([2,8,2])
b
prime
In [*]: data=[]
a=int(input("Enter the no of element "))
print("enter the no in array")
for i in range(int(a)):
n=input("num")
data.append(n)
print(data)
data.reverse()
print(data)
num
num
num
num
num
num
num
In [*]: n = 20
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
factorial(n)
In [ ]: