DataFrame object - это таблица с индексом. Задаем список дат (datetime), при помощи словаря задаем четыре столбца таблицы, вырезаем один столбец (Series)... следующий пример (Titanic): читаем csv-> DataFrame, еще пример "Olympic Medalists"
Python for Data Analysis Lightning Tutorials is a series of tutorials in Data Analysis, Statistics, and Graphics using Python. The Pandas Cookbook series of tutorials provides recipes for common tasks and moves on to more advanced topics in statistics and time series analysis.
Created by Alfred Essa, Dec 15th, 2013
Note: IPython Notebook and Data files can be found at my Github Site: http://github/alfredessa
Created by Alfred Essa, Dec 15th, 2013
Note: IPython Notebook and Data files can be found at my Github Site: http://github/alfredessa
In [8]:
from IPython.display import YouTubeVideo
YouTubeVideo('lhkchS9gSYk')
Out[8]:
Chapter 1: Data Structures
1.2 Problem. How can I create a DataFrame object in Pandas?
1.21 What is a DataFrame?
The DataFrame data structure in Pandas is a two-dimensional labeled array.- Data in the array can be of any type (integers, strings, floating point numbers, Python objects, etc.).
- Data within each column is homogeneous
- By default Pandas creates a numerical index for the rows in sequence 0...n
In [5]:
Image(filename='C:/Users/kiss/Documents/GitHub/pdacookbook/images/df1.jpg',width=400)
Out[5]:
Here's an example where we have set the Dates column to be the index and label for the rows.
In [6]:
Image(filename='C:/Users/kiss/Documents/GitHub/pdacookbook/images/df2.jpg',width=400)
Out[6]:
1.22 Preliminaries - import pandas and datetime library; create data for populating our first dataframe object
In [9]:
import pandas as pd
import datetime
In [10]:
# create a list containing dates from 12-01 to 12-07
dt = datetime.datetime(2013,12,1)
end = datetime.datetime(2013,12,8)
step = datetime.timedelta(days=1)
dates = []
In [11]:
# populate the list
while dt < end:
dates.append(dt.strftime('%m-%d'))
dt += step
In [12]:
dates
Out[12]:
In [13]:
d = {'Date': dates, 'Tokyo' : [15,19,15,11,9,8,13], 'Paris': [-2,0,2,5,7,-5,-3], 'Mumbai':[20,18,23,19,25,27,23]}
In [14]:
d
Out[14]:
1.23 Example 1: Create Dataframe Object from a Python Dictionary of equal length lists
In [15]:
temps = pd.DataFrame(d)
In [28]:
pd.DataFrame(d)
Out[28]:
In [16]:
ntemp = temps['Mumbai']
In [26]:
type(ntemp)
Out[26]:
In [17]:
# this is a Series (object)
ntemp
Out[17]:
In [18]:
temps = temps.set_index('Date')
In [19]:
temps
Out[19]:
1.24 Example 2 : Create DataFrame Object by reading a .csv file (Titanic passengers)
In [20]:
titanic = pd.read_csv('C:/Users/kiss/Documents/GitHub/pdacookbook/data/titanic.csv')
In [32]:
titanic.head()
Out[32]:
In [34]:
titanic.Sex.value_counts()
Out[34]:
In [21]:
titanic.Survived.value_counts()
Out[21]:
1.25 Example 3 : Create DataFrame Object by reading a .csv file (Olympic Medalists)
In [22]:
medals=pd.read_csv('C:/Users/kiss/Documents/GitHub/pdacookbook/data/olympicmedals.csv')
In [23]:
medals.tail()
Out[23]:
In [24]:
medals.Sport.value_counts()
Out[24]:
In [35]:
medals.NOC.value_counts()
Out[35]:
Посты чуть ниже также могут вас заинтересовать
Комментариев нет:
Отправить комментарий