pandas

Содержание
Введение
from_records(): чтение из кортежа
Объекты
Похожие статьи

Введение

import pandas as pd import numpy as np my_numpy_array = np.random.rand(3) my_numpy_array array([0.29042474, 0.25667264, 0.63304813]) type(my_numpy_array) <class 'numpy.ndarray'> my_first_df = pd.DataFrame(np.random.rand(3, 2)) my_first_df type(my_first_df) my_series = pd.Series(my_numpy_array) type(my_series) <class 'pandas.core.series.Series'> my_series[0] 0.29042473598604013 my_series = pd.Series(my_numpy_array, index=["First", "Second", "Third"]) my_series First 0.290425 Second 0.256673 Third 0.633048 dtype: float64 my_series.index Index(['First', 'Second', 'Third'], dtype='object') array_2d = np.random.rand(3,2) array_2d[0, 1] 0.6660297751929347 df = pd.DataFrame(array_2d) df 0 1 0 0.335997 0.666030 1 0.271107 0.890989 2 0.064006 0.829113 df[0,1] Traceback (most recent call last): File "<input>", line 1, in <module> File "/home/avorotyn/gitlab/perf_test/pycharm/pandas1/myenv/lib/python3.8/site-packages/pandas/core/frame.py", line 3024, in __getitem__ indexer = self.columns.get_loc(key) File "/home/avorotyn/gitlab/perf_test/pycharm/pandas1/myenv/lib/python3.8/site-packages/pandas/core/indexes/range.py", line 354, in get_loc raise KeyError(key) KeyError: (0, 1) df.columns RangeIndex(start=0, stop=2, step=1) df.columns = ["First", "Second"] df First Second 0 0.335997 0.666030 1 0.271107 0.890989 2 0.064006 0.829113 df["Second"] 0 0.666030 1 0.890989 2 0.829113 Name: Second, dtype: float64

from_records

>>> import pandas as pd
>>> sites = [("heihei.ru", "Туризм"), ("topbicyle.ru", "Велосипеды")]
>>> print(pd.DataFrame.from_records(sites))

0 1 0 heihei.ru Туризм 1 topbicyle.ru Велосипеды

Чтобы задать названия колонок воспользуйтесь параметром columns

pd.DataFrame.from_records(sites, columns=["URL", "Area"])

URL Area 0 heihei.ru Туризм 1 topbicyle.ru Велосипеды

Article author: Andrei Olegovich

Похожие статьи
Интерактивный режим
dict, list, str …
\: перенос строки
if, elif, else
Циклы
Функции
try except
Пакеты
ООП
Опеределить тип переменной Python
Работа с REST API на Python
Файлы: записать, прочитать…
Работа с базами данных
datetime: Дата и время в Python
json.dumps
Сложности при работе с Python
Фреймворки: Django, Flask
socket: Python Sockets
Виртуальное окружение
subprocess: bash команды из Python
multiprocessing: несколько процессов одновременно
psutil: cистемные ресурсы
sys.argv: аргументы командной строки
PyCharm: IDE
pydantic: валидация данных
paramiko: SSH из Python
logging: запись в лог
Обучение Python

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: