pandas

↓ По теме ↓
Introduction
from_records(): чтение из кортежа
Errors
Объекты

Introduction

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 Велосипеды

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

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

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

Article author: Andrei Olegovich

Related articles
Interactive mode
str: strings
\: long line wrapping
Lists []
if, elif, else
Loops
Methods
Functions
*args **kwargs
enum
Get Variable Type
Testing with Python
Calling REST API with Python
Files: write, read, append, context manager…
Download File
SQLite3: database
datetime: Date and Time in Python
json.dumps
Selenium + Python
Issues with Python
DJANGO
Flask
ZPL printer script
socket :Python Sockets
Virtual Environment
subprocess: shell commands execution
multiprocessing
psutil: system usage
sys.argv: command prompt variables
PyCharm: IDE
pydantic: data validation
paramiko: SSH with Python
enumerate

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to 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: