<jupyter_start><jupyter_text>![Imgur](https://i.imgur.com/5pXzCIu.png) Data Science va Sun'iy Intellekt Praktikum Ma'lumotlar tahlili. Pandas kutubxonasi. 2.2.2 `DataFrame` ma'lumotlar tuzilmasi `DataFrame` tartibli ustunlardan iborat, ikki o'lchovli (jadval ko'rinishidadi) ma'lumotlar tuzilmasi. Har bir ustun bir turdagi ma'lumotlarni saqlaydi (sonlar, matnlar, Boolean va hokazo). `DataFrame` ustunlari ham qatorlari ham indekslangan bo'ladi. Umuman olganda `DataFrame`ni bir nechta `Series`dan iboirat lug'atlar deb tasavvur qilishimiz mumkin.<jupyter_code>import pandas as pd
import numpy as np
from pandas import DataFrame<jupyter_output><empty_output><jupyter_text>`DataFrame` yaratishning bir nechta usuli bor va bulardan eng osoni qiymatlari bir hil uzunlikdagi ro'yxatdan iborat lug'at orqali yaratish.<jupyter_code>data = {'Yil':[2021, 2020, 2019, 2018, 2017, 2016, 2015, 2010],
       'Aholi soni':[33.9, 33.5, 32.9, 32.5, 31.9, 31.4, 30.9, 28.5],
       "Temp":[1.54, 1.48, 1.56, 1.62, 1.65, 1.66, 1.64, 1.53]}
df = DataFrame(data)
df<jupyter_output><empty_output><jupyter_text>Odatda katta `DataFrame`lar bilan ishlashda `.head()` metodi yordamida jadvalning boshidan 5 ta qator ko'rishimiz mumkin.<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Lug'atdan `DataFrame` yaratishda har bir qator indekslanadi (`0,1,2...`), ustunlar ketma-ketligi esa lug'atdagi ketma-ketlikka bog'liq bo'ladi. Ustunlar ketma-ketligini berishni istasak `DataFrame` yaratishda `columns` parametrini ham ko'rsatib ketishimiz mumkin:<jupyter_code>df = DataFrame(data, columns=['Yil','Temp','Aholi soni'])<jupyter_output><empty_output><jupyter_text>Agar `columns` ro'yxarida lug'atda mavjud bo'lmagan ustin bo'lsa, shu ustunga bo'sh qiymatlar qo'yib ketiladi.<jupyter_code>df = DataFrame(data, columns=['Yil','Temp','Aholi soni','YAIM'])
df.head()<jupyter_output><empty_output><jupyter_text>Huddi `Series` kabi `DataFrame` indekslariga ham o'zimiz qiymat berishimiz mumkin:<jupyter_code>df = DataFrame(data, columns=['Yil','Temp','Aholi soni','YAIM'], index=[21, 20, 19, 18, 17, 16, 15, 10])
df<jupyter_output><empty_output><jupyter_text>`DataFrame`ning istalgan ustunini `Series` ko'rinishida ajratib olishimiz mumkin. Buning ikki usuli bor:1. Ustun nomi attribut sifatida<jupyter_code>df.Temp<jupyter_output><empty_output><jupyter_text>2. Lug'at kabi ajratish<jupyter_code>df['Aholi soni']<jupyter_output><empty_output><jupyter_text>1-Usul faqatgina ustun nomi bir so'zdan iborat, Python o'zgaruvchi talablariga javob bergan holda ishlaydi.  Ahamiyat bering, ajratib olingan ustunlar indeksi va ustun nomi saqlanib qoladi Yuqoridagi usul bilan biz ustundagi barcha elementlarga qiymat berishimiz mumkin:<jupyter_code>df.YAIM = 1600
df<jupyter_output><empty_output><jupyter_text>`Series` yordamida biror ustunning ma'lum bir qatorlariga o'zgartirish kiritish mumkin. Bunda qatorlarning indeksi berilishi shart.<jupyter_code>val = pd.Series([1750, 1560, 1270], index=[21, 18, 10])
df.YAIM = val
df<jupyter_output><empty_output><jupyter_text>Shuningdek ustun qiymatlarini o'zgartirish uchun shu ustun qatorlari soniga teng ro'yxatdan foydalanishimiz mumkin.<jupyter_code>YAIM = [1700, 1670, 1600, 1590, 1400, 1300, 1350, 1360]
df['YAIM'] = YAIM
df<jupyter_output><empty_output><jupyter_text>Agar qiymat berishda mavjud bo'lmagan ustun nomi yozilsa, `DataFrame`ga yangi ustun qo'shiladi (bu faqat lug'at ko'rinishda yozishga xos). Quyidagi misolda biz avval yangi `1500+` degan ustun yaratdik keyin esa bu ustun qiymatlarini `df.YAIM>1500` sharti yordamida to'ldridik (ya'ni yalpi ichki mahsluot `1500` dan katta bo'lsa `True` aks holda 'False`.<jupyter_code>df["1500+"] = df.YAIM>1500
df<jupyter_output><empty_output><jupyter_text>Huddi lug'atlardagi kabi istalgan ustunni del operatori yordamida o'chirib tashlash ham mumkin.<jupyter_code>del df['1500+']
df<jupyter_output><empty_output><jupyter_text>`DataFrame` yaratishning yana bir usuli lug'atlar lug'atidan foydalanish. Bunda tashqi lug'at kalitlari ustun, ichki lug'at kalitlari esa indeks bo'ladi.<jupyter_code>data = {
    'Uzbekistan':{2021:33.9, 2020:33.5, 2019:32.9, 2018:32.5, 2017:31.9},
    'Kazakhstan':{2021:18.9, 2020:18.7, 2019:18.5, 2018:18.3, 2017:18.1, 2016:17.8}
}
df2 = DataFrame(data)
df2<jupyter_output><empty_output><jupyter_text>E'tibor bering, Uzbekistan ustunda 2016 yil uchun qiymat yo'q. Sababi nimada? Huddi `Series` kabi, `DataFrame` indeksini ham nomlashimiz mumkin.<jupyter_code>df2.index.name = "Yil"
df2<jupyter_output><empty_output><jupyter_text>Agar biz lug'atdan faqagina ma'lum bir qiymatlarni ajratib olishni istasak index ni o'zimiz ko'rsatishimiz mumkin.<jupyter_code>df2 = DataFrame(data, index=[2021, 2019, 2017]) # 2021, 2019 va 2017 yil ma'lumotlarini olamiz
df2<jupyter_output><empty_output><jupyter_text>Indekslar va Ustunlar o'rnini almashtirish uchun `.T` (transpose) metodidan foydalanish mumkin.<jupyter_code>df2.T<jupyter_output><empty_output><jupyter_text>`.values` parametri yordamida `DataFrame` ichidagi qiymatlarni 2-o'lchamli massiv ko'rinishida olishimiz mumkin.<jupyter_code>df2.values<jupyter_output><empty_output><jupyter_text>`Index` obyekti`Series` va `DataFrame` indeklasri ham maxsus Index obyekti hisoblanadi.<jupyter_code>df
indeks = df.index
indeks<jupyter_output><empty_output><jupyter_text>`Index` elementlariga tartib raqami bo'yciha murojaat qilishimiz mumkin<jupyter_code>indeks[0]
indeks[-1]
indeks[2:5]<jupyter_output><empty_output><jupyter_text>`Index` obyektining qiymatlarini o'zgartirib bo'lmaydi<jupyter_code>indeks[0]=22<jupyter_output><empty_output><jupyter_text>Buning afzalligi esa bir hil indeskli turli obyketlar (`DataFrame`lar) bilan ishlashni osonlashtiradi.Tasavvur qiling sizda turli yillar uchun turli ma'lumotlar turli jadvallarda (`DataFrame`larda) saqlanadi. Bu jadvallari o'zaro bog'lab turgna qiymat bu yillar. Agar yillardan indeks sifatida foydalansangiz, indesklar o'zgarmas bo'lishi hisobiga jadvallar o'rtasidagi bog'liqlik ham mustahkam bo'ladi.<jupyter_code>data1 = {
    'Uzbekistan':{2021:33.9, 2020:33.5, 2019:32.9, 2018:32.5, 2017:31.9},
    'Kazakhstan':{2021:18.9, 2020:18.7, 2019:18.5, 2018:18.3, 2017:18.1, 2016:17.8}
}
df = pd.DataFrame(data)
df
df.columns
'Tajikistan' in df.columns
2020 in df.index
indeks = df.index<jupyter_output><empty_output><jupyter_text>Python ro'yxatlaridan farqli ravishda `Index` takrorlanuvchi qiymatlar ega bo'lishi mumkin:<jupyter_code>usernames = {"anvar":"anvarbek223", "olim":"olimtoy", "maryam":"mary334455","javohir":"javohir445"}
emails = {"anvar":"<EMAIL>", "olim":"<EMAIL>", "maryam":"<EMAIL>","javohir":"<EMAIL>"}

data = {
    'username':usernames,
    'email':emails
}
df = pd.DataFrame(data)
df
data2 = {
    'username':{"olim":"olimjon21", "maryam":"maryam2020"},
    'email':{"olim":"<EMAIL>", "maryam":"<EMAIL>"}
}
df2 = df.append(pd.DataFrame(data2))
df2
df2.index<jupyter_output><empty_output><jupyter_text>Bazi `Index` metodlar va parametrlar<jupyter_code>newind = pd.Index(['olim','nafisa','hamida']) # yangi indeks yaratamiz
newind<jupyter_output><empty_output><jupyter_text>**Indeks boshqa indeks tarkbidia ekanini tekshirish**<jupyter_code>newind.isin(df2.index) # newind df2.index tarkibida borligni tekshirish<jupyter_output><empty_output><jupyter_text>**Indeksning boshqa indeksdan farqini tekshirish**<jupyter_code>df2.index.difference(newind) # df2
newind.difference(df2.index)<jupyter_output><empty_output><jupyter_text>**Ikki indeks to'plami**<jupyter_code>df2.index.union(newind)<jupyter_output><empty_output><jupyter_text>**`.delete()` va `.drop()`**<jupyter_code>df2.index.delete(0)
df2.index.drop('olim')
df2.index<jupyter_output><empty_output><jupyter_text>**`.insert()`**<jupyter_code>df2.index.insert(5,'masha') # 5-o'ringa mashani qo'shamiz<jupyter_output><empty_output><jupyter_text>`.is_monotonic` va `.is_unique`<jupyter_code>df2.index.is_monotonic
df2.index.is_unique
newind
newind.is_unique<jupyter_output><empty_output><jupyter_text>**`.unique()`**<jupyter_code>df2.index.unique() # noyob (takrorlanmas) elementlar<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12960217445021002, 'fraction_numerical': 0.05275512725475661, 'mean_word_length': 8.501173708920188, 'pattern_counts': {'":': 13, '<': 131, '<?xml version=': 0, '>': 133, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '31', 'dir': 'jupyter-structured-clean-dedup', 'id': '325490', 'license': 'MIT', 'n_tokens_mistral': 3841, 'n_tokens_neox': 3452, 'n_words': 621, 'path': 'Python/pandas/mohirdev/03-PANDAS-02-dars.ipynb', 'repo_name': 'YoshlikMedia/Data-Science', 'size': '8150'}