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

Extracted Code

Uploaded by

mayurspatel101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Extracted Code

Uploaded by

mayurspatel101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

import pandas as pd

import os

import matplotlib.pyplot as plt

# Initial data setup

d = {

'tno': ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'],

'tname': ['Tu Jhothi Me Makkar', 'No Time To Die', 'Minnal Murali', 'Red Alert',

'Saam Bahadur', 'Sanju', 'Teri Batoon Me Aisa Ulja Jiya'],

'tprice': [200, 300, 400, 300, 200, 400, 500],

'screen': [3, 2, 4, 2, 3, 6, 1],

'genre': ['Thriller', 'Action', 'Sci-Fiction', 'Thriller', 'Action',

'Action,Comedy', 'Romantic']

df = pd.DataFrame(d)

print('Ticket Information:')

print(df)

# Save to CSV

df.to_csv('tinfo.csv', index=False)

print('Menus available:')

print('1. Add a new row')

print('2. Search a row')

print('3. Update a row')


print('4. Delete a row')

print('5. Table without header')

print('6. Table without index')

print('7. Read the CSV file with new column names')

print('8. Access the values using head() function')

print('9. Access the values using tail() function')

print('10. Sorting in ascending order')

print('11. Sorting in descending order')

print('12. Display Movie Tickets where price is greater than 100')

print('13. Changing existing values to NaN')

print('14. Delete values using index')

print('15. Bar Graph')

print('16. Line Graph')

print('17. Histogram')

c = 'y'

while c == 'y':

ch = int(input('Enter your choice: '))

if ch == 1:

t = input('Enter ticket no: ')

tn = input('Enter the ticket name: ')

p = int(input('Enter the price: '))

sc = int(input('Enter the screen: '))

g = input('Enter the genre: ')

data = {'tno': [t], 'tname': [tn], 'tprice': [p], 'screen': [sc], 'genre': [g]}

new_df = pd.DataFrame(data)
df = pd.concat([df, new_df], ignore_index=True)

df.to_csv('tinfo.csv', index=False)

print(df)

elif ch == 2:

n = input('Enter the ticket no: ')

df = pd.read_csv('tinfo.csv')

s = df[df['tno'] == n]

print(s)

elif ch == 3:

N = input('Enter the ticket no: ')

df = pd.read_csv('tinfo.csv')

x = df[df['tno'] == N].index

if len(x) > 0:

print("Which column do you want to update?")

print("1. Ticket Name")

print("2. Price")

print("3. Screen")

print("4. Genre")

column_choice = int(input('Enter the column number: '))

if column_choice == 1:

new_value = input('Enter the new ticket name: ')

df.at[x[0], 'tname'] = new_value

elif column_choice == 2:

new_value = int(input('Enter the new price: '))


df.at[x[0], 'tprice'] = new_value

elif column_choice == 3:

new_value = int(input('Enter the new screen number: '))

df.at[x[0], 'screen'] = new_value

elif column_choice == 4:

new_value = input('Enter the new genre: ')

df.at[x[0], 'genre'] = new_value

else:

print('Invalid column number.')

df.to_csv('tinfo.csv', index=False)

print(df)

else:

print('Ticket not found.')

elif ch == 4:

df = pd.read_csv('tinfo.csv')

a = input('Enter the ticket name you want to delete: ')

if a in df['tname'].values:

df = df[df['tname'] != a]

df.to_csv('tinfo.csv', index=False)

print(df)

else:

print('Ticket name not found.')

elif ch == 5:

df = pd.read_csv('tinfo.csv', header=None)
print('Table without header:')

print(df)

elif ch == 6:

df = pd.read_csv('tinfo.csv', index_col=0)

print('Table without index:')

print(df)

elif ch == 7:

l = []

for i in range(5):

nn = input('Enter the new column name: ')

l.append(nn)

df = pd.read_csv('tinfo.csv', names=l, header=0)

print('Table with new column names:')

print(df)

elif ch == 8:

n = int(input('Number of values to be selected: '))

print('The first', n, 'values are:')

print(df.head(n))

elif ch == 9:

n = int(input('Number of values to be selected: '))

print('The last', n, 'values are:')

print(df.tail(n))
elif ch == 10:

cn = input('Enter the column name: ')

print('The table sorted in ascending order of', cn, ':')

print(df.sort_values(by=[cn]))

elif ch == 11:

cn = input('Enter the column name: ')

print('The table sorted in descending order of', cn, ':')

print(df.sort_values(by=[cn], ascending=False))

elif ch == 12:

df = pd.read_csv('tinfo.csv')

df1 = df[df['tprice'] > 100]

print(df1)

elif ch == 13:

n = int(input('Enter the number of values to be changed to NaN: '))

l = []

for i in range(n):

val = int(input('Enter the values to be changed: '))

l.append(val)

df = pd.read_csv('tinfo.csv', na_values=l)

print(df)

elif ch == 14:

n = int(input('Enter the number of indices to be removed: '))

l = []
for i in range(n):

val = int(input('Enter the index to be removed: '))

l.append(val)

print('Values after removal:')

print(df.drop(l))

elif ch == 15:

x = df['tname'].tolist()

y = df['tprice'].tolist()

plt.bar(x, y, color='b')

plt.xlabel('Movie')

plt.ylabel('Price')

plt.title('Bar Graph')

plt.show()

elif ch == 16:

x = df['tname'].tolist()

y = df['tprice'].tolist()

plt.xlabel('Movie')

plt.ylabel('Price')

plt.plot(x, y, '*r', linestyle='dotted')

plt.title('Line Chart')

plt.show()

elif ch == 17:

x = df['tprice'].tolist()

plt.hist(x, bins=7, color='red')


plt.xlabel('Price')

plt.ylabel('Frequency')

plt.title('Histogram')

plt.show()

else:

print('Invalid input')

c = input('Do you want to continue (y/n): ')

You might also like