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

Share in social media: