Skip to content

Instantly share code, notes, and snippets.

@d0uji
Last active July 12, 2018 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0uji/658082d2aee40c79c8eca29d4bb1d0de to your computer and use it in GitHub Desktop.
Save d0uji/658082d2aee40c79c8eca29d4bb1d0de to your computer and use it in GitHub Desktop.
# coding: cp1251
import pandas as pn
# block 1
with open("titanic.csv") as f: # открываем данные титаника
data = f.read()
df = pn.read_csv("titanic.csv") # читаем данные
print("Количество выживших:", sum(df['Survived']))
all_women_children = df[(df.Sex == "female") | (df.Age < 18)]
survived_women_children = df[((df.Sex == "female") | (df.Age < 18)) & (df.Survived == 1)]
print("Доля выживших женщин и детей: {0:.0%}".format(len(survived_women_children) / len(all_women_children)))
all_men = df[(df.Sex == "male")]
survived_men = df[(df.Sex == "male") & (df.Survived == 1)]
print("Доля выживших мужчин: {0:.0%}".format(len(survived_men) / len(all_men)))
first_class = df[(df.Pclass == 1)] # Не знаю правильно ли я понял, что первый класс это Pclass=1
print("Доля пассажиров первого класса: {0:.0%}".format(len(first_class) / len(df)))
children = df[(df.Age < 18)]
print("Доля детей: {0:.0%}".format(len(children) / len(df)))
# block 2
# 1
def y(x):
if x < 1:
y = (x**2-1)**2
elif x > 1:
y = 1 / (1+x)**2
elif x == 1:
y = 0
return y
a = float(input("Введите a: "))
b = float(input("Введите b: "))
h = float(input("Введите h: "))
df = pn.DataFrame(columns=["x", "y(x)"]) # задаём датафрейм
df.index.name = "Id"
i = 0
while a <= b:
x = a
df.loc[i] = [x, round(y(x), 5)] # записываем очередную строку результата
a += h
i += 1
# 2
print(df)
# 3
df.loc[len(df)] = [b+1, y(b+1)] # добавляем новую строку
print(df)
# 4
df = df.drop(0) # удаляем первую строку
print(df)
# 5
df = df[df.x >= b // 2] # удаляем строки, где x < b // 2
print(df)
# 6
df.to_csv("dataframe.csv", sep=",") # записываем всю хурму в файл
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment