0% found this document useful (0 votes)
18 views

Basic of Python

This document summarizes key Python, NumPy, and Pandas concepts. It shows how to split a string, use string formatting, access nested dictionary values, create arrays filled with zeros and other values, concatenate arrays, create DataFrames from lists, generate date ranges as series, and more in 3 sentences or less.

Uploaded by

Sudhakar HOD i/c
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Basic of Python

This document summarizes key Python, NumPy, and Pandas concepts. It shows how to split a string, use string formatting, access nested dictionary values, create arrays filled with zeros and other values, concatenate arrays, create DataFrames from lists, generate date ranges as series, and more in 3 sentences or less.

Uploaded by

Sudhakar HOD i/c
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic of PYTHON

Split this string


In [ ]:
s = "Hi there Sam!"
In [ ]:
s.split()
Out[ ]:
['Hi', 'there', 'Sam!']

2. Use .format() to print the following string.


Output should be: The diameter of Earth is 12742 kilometers.

In [ ]:
planet = "Earth"
diameter = 12742
print('The diameter of {} is {} kilometers. '. format(planet, diameter))
The diameter of Earth is 12742 kilometers.

3. In this nest dictionary grab the word "hello"


In [ ]:

d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':
[1,2,3,'hello']}]}]}
In [ ]:

d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':
[1,2,3,'hello']}]}]}
d['k1'][3]['tricky'][3]['target'][3]
Out[ ]:

'hello'

Numpy
import numpy as np

4.1 Create an array of 10 zeros?


4.2 Create an array of 10 fives?
print('The array of 10 zeros:')
np.zeros(10)

The array of 10 zeros:


Out[ ]:

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

print('The array of 10 fives:')


np.ones(10) *5

The array of 10 fives:


Out[ ]:

array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])

5. Create an array of all the even integers from 20 to


35
print(np.arange(20,35,2))
[20 22 24 26 28 30 32 34]

6. Create a 3x3 matrix with values ranging from 0 to 8


np.arange(0,9).reshape((3, 3))
Out[ ]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

7. Concatenate a and b

a = np.array([1, 2, 3]), b = np.array([4, 5, 6])


a=np.array([1,2,3])
b=np.array([4,5,6])
i=np.concatenate((a,b))
print(i)
[1 2 3 4 5 6]
Pandas

8. Create a dataframe with 3 rows and 2 columns


n [ ]:

import pandas as pd
In [ ]:

#initialize data
data=[['dillip','cse',80],['dhana','cse',90]]
df=pd.DataFrame(data,columns=['Name','Dept','marks'])
#print df
df
Out[ ]:

Name Dept marks

0 dillip cse 80

1 dhana cse 90

9. Generate the series of dates from 1st Jan, 2023 to 10th


Feb, 2023
In [ ]:

import pandas as pd
c=pd.date_range("01-01-2023","10-02-2023")
d=pd.Series(c)
print("The series of dates from 1st Jan, 2023 to 10th Feb, 2023")
d
The series of dates from 1st Jan, 2023 to 10th Feb, 2023
Out[ ]:

0 2023-01-01
1 2023-01-02
2 2023-01-03
3 2023-01-04
4 2023-01-05
...
270 2023-09-28
271 2023-09-29
272 2023-09-30
273 2023-10-01
274 2023-10-02
Length: 275, dtype: datetime64[ns]

10. Create 2D list to DataFrame


lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]
In [ ]:

lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]
In [ ]:

import pandas as pd
lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]
df=pd.DataFrame(lists)
#print df
df
Out[ ]:

0 1 2

0 1 aaa 22

bb
1 2 25
b

2 3 ccc 24

You might also like