Test Python Uta
Test Python Uta
Test Python Uta
lp=listOne[1::2]
print(lp)
li=listTwo[0::2]
print(li)
SAU:
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
print(df.head(5))
print(df.tail(5))
import pandas as pd
df = pd.read_csv('Automobile_data.csv')
df.replace(to_replace =["?", "n.a"],
value ="NaN")
print(df)
SAU:
import pandas as pd
df = pd.read_csv("Automobile_data.csv", na_values={
'price':["?","n.a"],
'stroke':["?","n.a"],
'horsepower':["?","n.a"],
'peak-rpm':["?","n.a"],
'average-mileage':["?","n.a"]})
print (df)
df.to_csv("Automobile_data.csv")
SAU:
# 5. Din fi?ierul CSV Automobile gasiti numele companiei cu masinile cele mai
scumpe �
listati numele companiei care are cea mai scumpa masina si pretul acesteia
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
df = df [['company','price']][df.price==df['price'].max()]
print(df)
# 6. Iterati lista de mai jos, numarati de c�te ori se �nt�lneste fiecare elemant
si
creati un dixtionar �n care sa aratati de c�te ori se �nt�lneste elemental
# 8. Din fi?ierul CSV Automobile listati detaliile tuturor masinilor Toyota Cars
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
car_Manufacturers = df.groupby('company')
toyotaDf = car_Manufacturers.get_group('toyota')
print(toyotaDf)
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
car_Manufacturers = df.groupby('company')
mileageDf = car_Manufacturers['company','average-mileage'].mean()
print(mileageDf)
# 10. Eliminati duplicatele dintr-o lista, creati un tuplu si gasiti numarul minim
si numarul maxim. De exemplu :
sampleList = [87, 45, 41, 65, 94, 41, 99, 94]
# eliminare duplicate
sampleList = list(dict.fromkeys(sampleList))
print(sampleList)
# creare tuplu
tuplu = tuple(sampleList)
print(tuplu)
# max si min
print(max(tuplu))
print(min(tuplu))
SAU:
# 11. Din fi?ierul CSV Automobile afisati numarul total de masini pe fiecare
companie
import pandas as pd
pd.read_csv('Automobile_data.csv', delimiter = ',')
auto = pd.read_csv('Automobile_data.csv', delimiter = ',')
print(auto.groupby('company').count())
SAU:
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
print(df['company'].value_counts())
# 12. Din fi?ierul CSV Automobile sortati toate masinile dupa coloana Price
# importing pandas package
import pandas as pd
# display
print(df)
SAU:
import pandas as pd
carsDf = pd.read_csv("Automobile_data.csv")
carsDf = carsDf.sort_values(by=['price', 'horsepower'], ascending=False)
print(carsDf)
# 13. Din fi?ierul CSV Automobile gasiti pentru fiecare companie masina cu cel mai
mare pret
import pandas as pd
pd.read_csv('Automobile_data.csv', delimiter = ',')
auto = pd.read_csv('Automobile_data.csv', delimiter = ',')
print(auto.groupby('company').max())
SAU:
import pandas as pd
df = pd.read_csv("Automobile_data.csv")
car_Manufacturers = df.groupby('company')
priceDf = car_Manufacturers['company','price'].max()
print(priceDf)
# 14. Create two data frames using following two Dicts, Merge two data frames, and
append second data frame as a new column to first data frame.
Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'Price': [23845, 17995,
135925 , 71400]}
car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'horsepower':
[141, 80, 182 , 160]}
# creare df
import pandas as pd
df1 = pd.DataFrame(Car_Price, columns = ['Company', 'Price'])
print(df1)
df2 = pd.DataFrame(car_Horsepower, columns=['Company', 'horsepower'])
print(df2)
SAU:
import pandas as pd
Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'Price': [23845, 17995,
135925 , 71400]}
carPriceDf = pd.DataFrame.from_dict(Car_Price)
car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'horsepower':
[141, 80, 182 , 160]}
carsHorsepowerDf = pd.DataFrame.from_dict(car_Horsepower)
carsDf = pd.merge(carPriceDf, carsHorsepowerDf, on="Company")
print(carsDf)
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("company_sales_data.csv")
profitList = df ['total_profit'].tolist()
monthList = df ['month_number'].tolist()
plt.plot(monthList, profitList, label = 'Month-wise Profit data of last year')
plt.xlabel('Month number')
plt.ylabel('Profit in dollar')
plt.xticks(monthList)
plt.title('Company profit per month')
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.show()
# 16. Calculate total sale data for last year for each product and show it using a
Pie chart (company_sales_data.csv file)
# Note: In Pie chart display Number of units sold per year for each product in
percentage. The Pie chart should look like this.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("company_sales_data.csv")
monthList = df ['month_number'].tolist()
labels = ['FaceCream', 'FaseWash', 'ToothPaste', 'Bathing soap', 'Shampoo',
'Moisturizer']
salesData = [df ['facecream'].sum(), df ['facewash'].sum(), df
['toothpaste'].sum(),
df ['bathingsoap'].sum(), df ['shampoo'].sum(), df ['moisturizer'].sum()]
plt.axis("equal")
plt.pie(salesData, labels=labels, autopct='%1.1f%%')
plt.legend(loc='lower right')
plt.title('Sales data')
plt.show()
https://pynative.com/python-exercises-with-solutions/