首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python--pandas数据创建

Python--pandas数据创建

作者头像
披头
发布2019-12-26 10:51:36
8140
发布2019-12-26 10:51:36
举报
文章被收录于专栏:datartisandatartisandatartisan

pandas 有两种数据结构

series:一维列表,带有标签的同构类型数组 ;

DataFrame:二维列表,带有标签的可包含异构类型、大小可变的数据列,表格结构;

In [2]:

# series创建
 
import pandas as pd
import numpy as np
 
series1 = pd.Series([1, 2, 3, 4])
series1

Out[2]:

0    1
1    2
2    3
3    4
dtype: int64

输出的最后一行是Series中数据的类型,这里的数据都是int64类型的。 数据在第二列输出,第一列是数据的索引,在pandas中称之为Index。

In [3]:

series1.index

Out[3]:

RangeIndex(start=0, stop=4, step=1)

In [4]:

series1.values

Out[4]:

array([1, 2, 3, 4], dtype=int64)

默认情况下,index是[0,n-1]的形式。我们可以自定义索引值,索引值可以是任意类型

In [5]:

series2 = pd.Series([1, 2, 3, 4],
                   index=['a', 'b', 'c', 'd'])
series2

Out[5]:

a    1
b    2
c    3
d    4
dtype: int64

In [6]:

# Create DataFrame from Dictionary using default Constructor
# 通过字典创建DataFrame
studentData = {
'name' : ['jack', 'Riti', 'Aadi'],
'age' : [34, 30, 16],
'city' : ['Sydney', 'Delhi', 'New york']
}

In [8]:

df = pd.DataFrame(studentData) 
df

Out[8]:

name

age

city

0

jack

34

Sydney

1

Riti

30

Delhi

2

Aadi

16

New york

In [9]:

# 创建时自定义索引
df = pd.DataFrame(studentData, index=['a', 'b', 'c'])
df

Out[9]:

name

age

city

a

jack

34

Sydney

b

Riti

30

Delhi

c

Aadi

16

New york

In [15]:

# Create DataFrame from not compatible dictionary 
# 单列字典创建DataFrame

studentAgeData = {
'Jack' : 12,
'Roma' : 13,
'Ritika' : 10,
'Aadi' : 11
}
# df = pd.DataFrame(studentAgeData)
df = pd.DataFrame(list(studentAgeData.items()), index=['a', 'b', 'c', 'd'])
df

Out[15]:

0

1

a

Jack

12

b

Roma

13

c

Ritika

10

d

Aadi

11

In [16]:

# Create DataFrame from Dictionary and skip data 
# 跳过某列创建DataFrame

studentData = {
'name' : ['jack', 'Riti', 'Aadi'],
'age' : [34, 30, 16],
'city' : ['Sydney', 'Delhi', 'New york']
}

In [19]:

# Creating Dataframe from Dictionary by Skipping 2nd Item from dict 
# 跳过某列

dfObj = pd.DataFrame(studentData, columns=['name', 'city'])
dfObj

Out[19]:

name

city

0

jack

Sydney

1

Riti

Delhi

2

Aadi

New york

In [20]:

# Create DataFrame from Dictionary with different Orientation 
# 不同方向
studentData = {
'name' : ['jack', 'Riti', 'Aadi'],
'age' : [34, 30, 16],
'city' : ['Sydney', 'Delhi', 'New york']
}

In [21]:

# Create dataframe from dic and make keys, index in dataframe
dfObj = pd.DataFrame.from_dict(studentData, orient='index')
dfObj

Out[21]:

0

1

2

name

jack

Riti

Aadi

age

34

30

16

city

Sydney

Delhi

New york

In [24]:

# Create DataFrame from nested Dictionary 
# 包含嵌套的字典
studentData = { 
}
0 : {
'name' : 'Aadi',
'age' : 16,
'city' : 'New york'
    },
1 : {
'name' : 'Jack',
'age' : 34,
'city' : 'Sydney'
    },
2 : {
'name' : 'Riti',
'age' : 30,
'city' : 'Delhi'
    }

In [25]:

# Create dataframe from nested dictionary 
# 包含嵌套的字典

dfObj = pd.DataFrame(studentData)
dfObj

Out[25]:

0

1

2

age

16

34

30

city

New york

Sydney

Delhi

name

Aadi

Jack

Riti

In [26]:

# Transpose dataframe object 
# 行列转换

dfObj = dfObj.transpose()
dfObj

Out[26]:

age

city

name

0

16

New york

Aadi

1

34

Sydney

Jack

2

30

Delhi

Riti

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 乐享数据8090 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档