如何给笔记本更好散热,数据科学 IPython 笔记本 7.1 Pandas

 2023-09-25 阅读 13 评论 0

摘要:7.1 Pandas 原文:Pandas 译者:飞龙 协议:CC BY-NC-SA 4.0 致谢:这个笔记摘自 Wes McKinney 的著作 《Python 数据分析》(Python for Data Analysis) 序列(Series)数据帧(DataFrame)重索引删除条目索引

7.1 Pandas

原文:Pandas

译者:飞龙

协议:CC BY-NC-SA 4.0

致谢:这个笔记摘自 Wes McKinney 的著作 《Python 数据分析》(Python for Data Analysis)

  • 序列(Series
  • 数据帧(DataFrame
  • 重索引
  • 删除条目
  • 索引,选择和过滤
  • 算术和数据对齐
  • 函数应用和映射
  • 排序和排名
  • 带有重复值的轴索引
  • 汇总和计算描述性统计量
  • 清洗数据(构建中)
  • 输入和输出(构建中)
from pandas import Series, DataFrame
import pandas as pd
import numpy as np

序列(Series

如何给笔记本更好散热。Series是一维数组对象,包含数据数组和相关的数据标签数组。数据可以是任何 NumPy 数据类型,标签是序列的索引。

创建Series

ser_1 = Series([1, 1, 2, -3, -5, 8, 13])
ser_1'''
0     1
1     1
2     2
3    -3
4    -5
5     8
6    13
dtype: int64
'''

获取Series的数组表示:

ser_1.values# array([ 1,  1,  2, -3, -5,  8, 13])

Series对象是不可变的,并持有轴标签和元数据,如名称和轴名称。

获取Series的索引:

ser_1.index# Int64Index([0, 1, 2, 3, 4, 5, 6], dtype='int64')

适合数据分析的笔记本。使用自定义索引创建Series

ser_2 = Series([1, 1, 2, -3, -5], index=['a', 'b', 'c', 'd', 'e'])
ser_2'''
a    1
b    1
c    2
d   -3
e   -5
dtype: int64
'''

Series获取值:

ser_2[4] == ser_2['e']# True

通过传入列表从Series获取一组值:

ser_2[['c', 'a', 'b']]'''
c    2
a    1
b    1
dtype: int64
'''

获取大于 0 的值:

ser_2[ser_2 > 0]'''
a    1
b    1
c    2
dtype: int64
'''

标量乘法:

ser_2 * 2'''
a     2
b     2
c     4
d    -6
e   -10
dtype: int64
'''

买笔记本计算机、应用 NumPy 数学函数:

import numpy as np
np.exp(ser_2)'''
a    2.718282
b    2.718282
c    7.389056
d    0.049787
e    0.006738
dtype: float64
'''

Series就像一个固定长度的有序字典。

传入字典来创建Series

dict_1 = {'foo' : 100, 'bar' : 200, 'baz' : 300}
ser_3 = Series(dict_1)
ser_3'''
bar    200
baz    300
foo    100
dtype: int64
'''

通过传入索引来重排Series(未找到的索引是 NaN):

index = ['foo', 'bar', 'baz', 'qux']
ser_4 = Series(dict_1, index=index)
ser_4'''
foo    100
bar    200
baz    300
qux    NaN
dtype: float64
'''

使用 pandas 方法检查 NaN:

pd.isnull(ser_4)'''
foo    False
bar    False
baz    False
qux     True
dtype: bool
'''

python数据科学之路,使用Series的方法检查 NaN:

ser_4.isnull()'''
foo    False
bar    False
baz    False
qux     True
dtype: bool
'''

在算术运算中,Series自动对齐不同的索引数据:

ser_3 + ser_4'''
bar    400
baz    600
foo    200
qux    NaN
dtype: float64
'''

命名Series

ser_4.name = 'foobarbazqux'

命名Series索引:

ser_4.index.name = 'label'ser_4'''
label
foo    100
bar    200
baz    300
qux    NaN
Name: foobarbazqux, dtype: float64
'''

原地重命名Series的索引:

ser_4.index = ['fo', 'br', 'bz', 'qx']
ser_4'''
fo    100
br    200
bz    300
qx    NaN
Name: foobarbazqux, dtype: float64
'''

数据帧(DataFrame

宏碁笔记本怎么样,DataFrame是表格数据结构,包含列的有序集合。 每列可以是不同的类型。 DataFrame同时具有行索引和列索引,类似于Series的字典。行和列操作大致是对称实现的。 索引DataFrame时返回的列是底层数据的视图,而不是副本。 要获取副本,请使用Series的复制方法。

创建DataFrame:

data_1 = {'state' : ['VA', 'VA', 'VA', 'MD', 'MD'],'year' : [2012, 2013, 2014, 2014, 2015],'pop' : [5.0, 5.1, 5.2, 4.0, 4.1]}
df_1 = DataFrame(data_1)
df_1
popstateyear
05.0VA2012
15.1VA2013
25.2VA2014
34.0MD2014
44.1MD2015

指定列的序列来创建DataFrame

df_2 = DataFrame(data_1, columns=['year', 'state', 'pop'])
df_2

| | year | state | pop |
| — | — | — |
| 0 | 2012 | VA | 5.0 |
| 1 | 2013 | VA | 5.1 |
| 2 | 2014 | VA | 5.2 |
| 3 | 2014 | MD | 4.0 |
| 4 | 2015 | MD | 4.1 |

Series类似,数据中不存在的列是 NaN:

df_3 = DataFrame(data_1, columns=['year', 'state', 'pop', 'unempl'])
df_3
yearstatepopunempl
02012VA5.0NaN
12013VA5.1NaN
22014VA5.2NaN
32014MD4.0NaN
42015MD4.1NaN

笔记本电脑的参数知识。通过键检索列,返回Series

df_3['state']'''
0    VA
1    VA
2    VA
3    MD
4    MD
Name: state, dtype: object
'''

通过属性检索列,返回Series

df_3.year'''
0    2012
1    2013
2    2014
3    2014
4    2015
Name: year, dtype: int64
'''

通过位置检索行:

df_3.ix[0]'''
year      2012
state       VA
pop          5
unempl     NaN
Name: 0, dtype: object
'''

通过复制来更新列:

df_3['unempl'] = np.arange(5)
df_3

| | year | state | pop | unempl |
| — | — | — | — |
| 0 | 2012 | VA | 5.0 | 0 |
| 1 | 2013 | VA | 5.1 | 1 |
| 2 | 2014 | VA | 5.2 | 2 |
| 3 | 2014 | MD | 4.0 | 3 |
| 4 | 2015 | MD | 4.1 | 4 |

电脑笔记本?将Series赋给列(请注意,如果指定了列表或数组,则长度必须与DataFrame匹配,与Series不同):

unempl = Series([6.0, 6.0, 6.1], index=[2, 3, 4])
df_3['unempl'] = unempl
df_3
yearstatepopunempl
02012VA5.0NaN
12013VA5.1NaN
22014VA5.26.0
32014MD4.06.0
42015MD4.16.1

对不存在的新列赋值来创建新列:

df_3['state_dup'] = df_3['state']
df_3
yearstatepopunemplstate_dup
02012VA5.0NaNVA
12013VA5.1NaNVA
22014VA5.26.0VA
32014MD4.06.0MD
42015MD4.16.1MD

删除一列:

del df_3['state_dup']
df_3
yearstatepopunempl
02012VA5.0NaN
12013VA5.1NaN
22014VA5.26.0
32014MD4.06.0
42015MD4.16.1

从字典的嵌套字典创建DataFrame(如果没有指定显示索引,内部字典中的键,被合并并排序来形成结果中的索引):

pop = {'VA' : {2013 : 5.1, 2014 : 5.2},'MD' : {2014 : 4.0, 2015 : 4.1}}
df_4 = DataFrame(pop)
df_4
MDVA
2013NaN5.1
20144.05.2
20154.1NaN

转置DataFrame:

df_4.T
201320142015
MDNaN4.04.1
VA5.15.2NaN

笔记本电脑配置怎么看。从Series的词典创建一个DataFrame

data_2 = {'VA' : df_4['VA'][1:],'MD' : df_4['MD'][2:]}
df_5 = DataFrame(data_2)
df_5
MDVA
2014NaN5.2
20154.1NaN

设置DataFrame的索引名称:

df_5.index.name = 'year'
df_5
MDVA
year
2014NaN5.2
20154.1NaN

设置DataFrame的列名称:

df_5.columns.name = 'state'
df_5
stateMDVA
year
2014NaN5.2
20154.1NaN

DataFrame中包含的数据作为 2D ndarray返回:

df_5.values'''
array([[ nan,  5.2],[ 4.1,  nan]])
'''

如果列是不同的dtypes,则 2D 数组的dtype将兼容所有列:

df_3.values'''
array([[2012, 'VA', 5.0, nan],[2013, 'VA', 5.1, nan],[2014, 'VA', 5.2, 6.0],[2014, 'MD', 4.0, 6.0],[2015, 'MD', 4.1, 6.1]], dtype=object)
'''

重索引

新手如何买笔记本电脑、使用符合新索引的数据创建新对象。任何缺失值都设置为 NaN。

df_3
yearstatepopunempl
02012VA5.0NaN
12013VA5.1NaN
22014VA5.26.0
32014MD4.06.0
42015MD4.16.1

行的重新索引将返回具有指定索引的新DataFrame

df_3.reindex(list(reversed(range(0, 6))))
yearstatepopunempl
5NaNNaNNaNNaN
42015MD4.16.1
32014MD4.06.0
22014VA5.26.0
12013VA5.1NaN
02012VA5.0NaN

缺失值可以设置为 NaN 以外的值:

df_3.reindex(range(6, 0), fill_value=0)
yearstatepopunempl

插入有序数据,如时间序列:

ser_5 = Series(['foo', 'bar', 'baz'], index=[0, 2, 4])ser_5.reindex(range(5), method='ffill')'''
0    foo
1    foo
2    bar
3    bar
4    baz
dtype: object
'''ser_5.reindex(range(5), method='bfill')'''
0    foo
1    bar
2    bar
3    baz
4    baz
dtype: object
'''

重索引列:

df_3.reindex(columns=['state', 'pop', 'unempl', 'year'])
statepopunemplyear
0VA5.0NaN2012
1VA5.1NaN2013
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015

华硕笔记本,重新索引行和列,同时填充行:

df_3.reindex(index=list(reversed(range(0, 6))),fill_value=0,columns=['state', 'pop', 'unempl', 'year'])
statepopunemplyear
500.00.00
4MD4.16.12015
3MD4.06.02014
2VA5.26.02014
1VA5.1NaN2013
0VA5.0NaN2012

使用ix的重索引:

df_6 = df_3.ix[range(0, 7), ['state', 'pop', 'unempl', 'year']]
df_6
statepopunemplyear
0VA5.0NaN2012
1VA5.1NaN2013
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015
5NaNNaNNaNNaN
6NaNNaNNaNNaN

删除条目

SeriesDataFrame中删除行:

df_7 = df_6.drop([0, 1])
df_7
statepopunemplyear
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015
5NaNNaNNaNNaN
6NaNNaNNaNNaN

DataFrame中删除列:

df_7 = df_7.drop('unempl', axis=1)
df_7
statepopyear
2VA5.22014
3MD4.02014
4MD4.12015
5NaNNaNNaN
6NaNNaNNaN

索引,选择和过滤

Series索引类似于 NumPy 数组索引,并且能够使用Series的索引值。

ser_2'''
a    1
b    1
c    2
d   -3
e   -5
dtype: int64
'''

python的科学计算库有哪些。从Series中选择值:

ser_2[0] == ser_2['a']# True

Series中选择切片:

ser_2[1:4]'''
b    1
c    2
d   -3
dtype: int64
'''

Series中选择特定值:

ser_2[['b', 'c', 'd']]'''
b    1
c    2
d   -3
dtype: int64
'''

基于过滤器从Series中选择值:

ser_2[ser_2 > 0]'''
a    1
b    1
c    2
dtype: int64
'''

从带标签的Series中选择切片(注意包含终点):

ser_2['a':'b']'''
a    1
b    1
dtype: int64
'''

推荐个笔记本电脑。对Series切片赋值(注意包含终点):

ser_2['a':'b'] = 0
ser_2'''
a    0
b    0
c    2
d   -3
e   -5
dtype: int64
'''

Pandas 支持DataFrame中的索引。

df_6
statepopunemplyear
0VA5.0NaN2012
1VA5.1NaN2013
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015
5NaNNaNNaNNaN
6NaNNaNNaNNaN

DataFrame中选择特定列:

df_6[['pop', 'unempl']]
popunempl
05.0NaN
15.1NaN
25.26.0
34.06.0
44.16.1
5NaNNaN
6NaNNaN

DataFrame中选择切片:

df_6[:2]
statepopunemplyear
0VA5.0NaN2012
1VA5.1NaN2013

基于过滤器从DataFrame中选择行:

df_6[df_6['pop'] > 5]
statepopunemplyear
1VA5.1NaN2013
2VA5.262014

DataFrame上执行标量比较:

df_6 > 5
statepopunemplyear
0TrueFalseFalseTrue
1TrueTrueFalseTrue
2TrueTrueTrueTrue
3TrueFalseTrueTrue
4TrueFalseTrueTrue
5TrueFalseFalseFalse
6TrueFalseFalseFalse

DataFrame上执行标量比较,保留满足过滤器的行:

df_6[df_6 > 5]
statepopunemplyear
0VANaNNaN2012
1VA5.1NaN2013
2VA5.26.02014
3MDNaN6.02014
4MDNaN6.12015
5NaNNaNNaNNaN
6NaNNaNNaNNaN

DataFrame中选择一行(注意包含终点):

df_6.ix[2:3]
statepopunemplyear
2VA5.262014
3MD4.062014

DataFrame的特定列中选择行的切片:

df_6.ix[0:2, 'pop']'''
0    5.0
1    5.1
2    5.2
Name: pop, dtype: float64
'''

根据特定行上的算术运算选择行:

df_6.ix[df_6.unempl > 5.0]
statepopunemplyear
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015

算术和数据对齐

如果索引对不相同,则将Series对象相加会产生索引对的并集,使不重叠的索引为 NaN:

np.random.seed(0)
ser_6 = Series(np.random.randn(5),index=['a', 'b', 'c', 'd', 'e'])
ser_6'''
a    1.764052
b    0.400157
c    0.978738
d    2.240893
e    1.867558
dtype: float64
'''np.random.seed(1)
ser_7 = Series(np.random.randn(5),index=['a', 'c', 'e', 'f', 'g'])
ser_7'''
a    1.624345
c   -0.611756
e   -0.528172
f   -1.072969
g    0.865408
dtype: float64
'''ser_6 + ser_7'''
a    3.388398
b         NaN
c    0.366982
d         NaN
e    1.339386
f         NaN
g         NaN
dtype: float64
'''

为不重叠的索引设置填充值而不是 NaN:

ser_6.add(ser_7, fill_value=0)'''
a    3.388398
b    0.400157
c    0.366982
d    2.240893
e    1.339386
f   -1.072969
g    0.865408
dtype: float64
'''

如果索引对不相同,则将DataFrame对象相加,会产生行和列的索引对的并集,使不重叠的索引为 NaN:

np.random.seed(0)
df_8 = DataFrame(np.random.rand(9).reshape((3, 3)),columns=['a', 'b', 'c'])
df_8
abc
00.5488140.7151890.602763
10.5448830.4236550.645894
20.4375870.8917730.963663
np.random.seed(1)
df_9 = DataFrame(np.random.rand(9).reshape((3, 3)),columns=['b', 'c', 'd'])
df_9
bcd
00.4170220.7203240.000114
10.3023330.1467560.092339
20.1862600.3455610.396767
df_8 + df_9
abcd
0NaN1.1322111.323088NaN
1NaN0.7259870.792650NaN
2NaN1.0780331.309223NaN

为不重叠的索引设置填充值而不是 NaN:

df_10 = df_8.add(df_9, fill_value=0)
df_10
abcd
00.5488141.1322111.3230880.000114
10.5448830.7259870.7926500.092339
20.4375871.0780331.3092230.396767

与 NumPy 一样,pandas 支持DataFrameSeries之间的算术运算。

DataFrame的列上匹配Series的索引,并向下广播行:

ser_8 = df_10.ix[0]
df_11 = df_10 - ser_8
df_11
abcd
00.0000000.0000000.0000000.000000
1-0.003930-0.406224-0.5304380.092224
2-0.111226-0.054178-0.0138640.396653

DataFrame的列上匹配Series的索引,向下广播行并合并不匹配的索引:

ser_9 = Series(range(3), index=['a', 'd', 'e'])
ser_9'''
a    0
d    1
e    2
dtype: int64
'''df_11 - ser_9
abcde
00.000000NaNNaN-1.000000NaN
1-0.003930NaNNaN-0.907776NaN
2-0.111226NaNNaN-0.603347NaN

使用算术方法,在列上广播并匹配行(axis = 0):

df_10
abcd
00.5488141.1322111.3230880.000114
10.5448830.7259870.7926500.092339
20.4375871.0780331.3092230.396767
ser_10 = Series([100, 200, 300])
ser_10'''
0    100
1    200
2    300
dtype: int64
'''df_10.sub(ser_10, axis=0)
abcd
0-99.451186-98.867789-98.676912-99.999886
1-199.455117-199.274013-199.207350-199.907661
2-299.562413-298.921967-298.690777-299.603233

函数应用和映射

NumPy ufunc(逐元素数组方法)能够操作 pandas 对象:

df_11 = np.abs(df_11)
df_11
abcd
00.0000000.0000000.0000000.000000
10.0039300.4062240.5304380.092224
20.1112260.0541780.0138640.396653

将 1D 数组上的函数应用于每列:

func_1 = lambda x: x.max() - x.min()
df_11.apply(func_1)'''
a    0.111226
b    0.406224
c    0.530438
d    0.396653
dtype: float64
'''

将 1D 数组上的函数应用于每行:

df_11.apply(func_1, axis=1)'''
0    0.000000
1    0.526508
2    0.382789
dtype: float64
'''

应用函数并返回DataFrame:

func_2 = lambda x: Series([x.min(), x.max()], index=['min', 'max'])
df_11.apply(func_2)
abcd
min0.0000000.0000000.0000000.000000
max0.1112260.4062240.5304380.396653

将逐元素的 Python 函数应用于DataFrame

func_3 = lambda x: '%.2f' %x
df_11.applymap(func_3)
abcd
00.000.000.000.00
10.000.410.530.09
20.110.050.010.40

将逐元素的 Python 函数应用于Series

df_11['a'].map(func_3)'''
0    0.00
1    0.00
2    0.11
Name: a, dtype: object
'''

排序和排名

ser_4'''
fo    100
br    200
bz    300
qx    NaN
Name: foobarbazqux, dtype: float64
'''

按照索引排序Series

ser_4.sort_index()'''
br    200
bz    300
fo    100
qx    NaN
Name: foobarbazqux, dtype: float64
'''

按照值排序Series

ser_4.sort_values()'''
fo    100
br    200
bz    300
qx    NaN
Name: foobarbazqux, dtype: float64
'''df_12 = DataFrame(np.arange(12).reshape((3, 4)),index=['three', 'one', 'two'],columns=['c', 'a', 'b', 'd'])
df_12
cabd
three0123
one4567
two891011

按照索引排序DaraFrame

df_12.sort_index()
cabd
one4567
three0123
two891011

按列倒序排序DaraFrame

df_12.sort_index(axis=1, ascending=False)
dcba
three3021
one7465
two118109

按列排序DaraFrame的值:

df_12.sort_values(by=['d', 'c'])
cabd
three0123
one4567
two891011

排名类似于numpy.argsort,除了通过为每个组分配平均排名来打破关系:

ser_11 = Series([7, -5, 7, 4, 2, 0, 4, 7])
ser_11 = ser_11.sort_values()
ser_11'''
1   -5
5    0
4    2
3    4
6    4
0    7
2    7
7    7
dtype: int64
'''ser_11.rank()'''
1    1.0
5    2.0
4    3.0
3    4.5
6    4.5
0    7.0
2    7.0
7    7.0
dtype: float64
'''

根据数据出现在Series中的位置,来排名Series

ser_11.rank(method='first')'''
1    1
5    2
4    3
3    4
6    5
0    6
2    7
7    8
dtype: float64
'''

使用分组的最大排名,降序排列Series

ser_11.rank(ascending=False, method='max')'''
1    8
5    7
4    6
3    5
6    5
0    3
2    3
7    3
dtype: float64
'''

DataFrame可以按行或列排名。

df_13 = DataFrame({'foo' : [7, -5, 7, 4, 2, 0, 4, 7],'bar' : [-5, 4, 2, 0, 4, 7, 7, 8],'baz' : [-1, 2, 3, 0, 5, 9, 9, 5]})
df_13
barbazfoo
0-5-17
142-5
2237
3004
4452
5790
6794
7857

在行上(按列)排名DataFrame

df_13.rank()
barbazfoo
01.01.07.0
14.53.01.0
23.04.07.0
32.02.04.5
44.55.53.0
56.57.52.0
66.57.54.5
78.05.57.0

在列上(按行)排名DataFrame

df_13.rank(axis=1)
barbazfoo
01.02.03
13.02.01
21.02.03
31.51.53
42.03.01
52.03.01
62.03.01
73.01.02

带有重复值的轴索引

标签在 Pandas 中不一定是唯一的:

ser_12 = Series(range(5), index=['foo', 'foo', 'bar', 'bar', 'baz'])
ser_12'''
foo    0
foo    1
bar    2
bar    3
baz    4
dtype: int64
'''ser_12.index.is_unique# False

选择序列的元素:

ser_12['foo']'''
foo    0
foo    1
dtype: int64
'''

选择DataFrame的元素:

df_14 = DataFrame(np.random.randn(5, 4),index=['foo', 'foo', 'bar', 'bar', 'baz'])
df_14
0123
foo-2.3634691.135345-1.0170140.637362
foo-0.8599071.772608-1.1103630.181214
bar0.564345-0.5665100.7299760.372994
bar0.533811-0.0919731.9138200.330797
baz1.141943-1.129595-0.8500520.960820
df_14.ix['bar']
0123
bar0.564345-0.5665100.7299760.372994
bar0.533811-0.0919731.9138200.330797

汇总和计算描述性统计量

与 NumPy 数组不同,Pandas 描述性统计量会自动排除缺失数据。 除非整行或列为 NA,否则将排除 NaN 值。

df_6
statepopunemplyear
0VA5.0NaN2012
1VA5.1NaN2013
2VA5.26.02014
3MD4.06.02014
4MD4.16.12015
5NaNNaNNaNNaN
6NaNNaNNaNNaN
df_6.sum()'''
pop          23.4
unempl       18.1
year      10068.0
dtype: float64
'''

按行求和:

df_6.sum(axis=1)'''
0    2017.0
1    2018.1
2    2025.2
3    2024.0
4    2025.2
5       0.0
6       0.0
dtype: float64
'''

计入 NaN:

df_6.sum(axis=1, skipna=False)'''
0       NaN
1       NaN
2    2025.2
3    2024.0
4    2025.2
5       NaN
6       NaN
dtype: float64
'''

清洗数据(构建中)

  • 替换
  • 删除
  • 连接
from pandas import Series, DataFrame
import pandas as pd

创建DataFrame

data_1 = {'state' : ['VA', 'VA', 'VA', 'MD', 'MD'],'year' : [2012, 2013, 2014, 2014, 2015],'population' : [5.0, 5.1, 5.2, 4.0, 4.1]}
df_1 = DataFrame(data_1)
df_1
populationstateyear
05.0VA2012
15.1VA2013
25.2VA2014
34.0MD2014
44.1MD2015

替换

将字符串的所有出现替换为另一个字符串(不复制):

df_1.replace('VA', 'VIRGINIA', inplace=True)
df_1
populationstateyear
05.0VIRGINIA2012
15.1VIRGINIA2013
25.2VIRGINIA2014
34.0MD2014
44.1MD2015

在指定的列中,将字符串的所有出现替换为另一个字符串(不复制):

df_1.replace({'state' : { 'MD' : 'MARYLAND' }}, inplace=True)
df_1
populationstateyear
05.0VIRGINIA2012
15.1VIRGINIA2013
25.2VIRGINIA2014
34.0MARYLAND2014
44.1MARYLAND2015

删除

删除'population'列并返回DataFrame的副本:

df_2 = df_1.drop('population', axis=1)
df_2
stateyear
0VIRGINIA2012
1VIRGINIA2013
2VIRGINIA2014
3MARYLAND2014
4MARYLAND2015

连接

连接两个DaraFrame

data_2 = {'state' : ['NY', 'NY', 'NY', 'FL', 'FL'],'year' : [2012, 2013, 2014, 2014, 2015],'population' : [6.0, 6.1, 6.2, 3.0, 3.1]}
df_3 = DataFrame(data_2)
df_3
populationstateyear
06.0NY2012
16.1NY2013
26.2NY2014
33.0FL2014
43.1FL2015
df_4 = pd.concat([df_1, df_3])
df_4
populationstateyear
05.0VIRGINIA2012
15.1VIRGINIA2013
25.2VIRGINIA2014
34.0MARYLAND2014
44.1MARYLAND2015
06.0NY2012
16.1NY2013
26.2NY2014
33.0FL2014
43.1FL2015

输入和输出(构建中)

from pandas import Series, DataFrame
import pandas as pd

将 CSV 文件中的数据读入DataFrame(对 TSV 使用sep='\t'):

df_1 = pd.read_csv("../data/ozone.csv")

获取DataFrame的摘要:

df_1.describe()
OzoneSolar.RWindTempMonthDay
count116.000000146.000000153.000000153.000000153.000000153.000000
mean42.129310185.9315079.95751677.8823536.99346415.803922
std32.98788590.0584223.5230019.4652701.4165228.864520
min1.0000007.0000001.70000056.0000005.0000001.000000
25%18.000000115.7500007.40000072.0000006.0000008.000000
50%31.500000205.0000009.70000079.0000007.00000016.000000
75%63.250000258.75000011.50000085.0000008.00000023.000000
max168.000000334.00000020.70000097.0000009.00000031.000000

列出DataFrame的前五行:

df_1.head()
OzoneSolar.RWindTempMonthDay
0411907.46751
1361188.07252
21214912.67453
31831311.56254
4NaNNaN14.35655

创建 CSV 文件的副本,以 UTF-8 编码并隐藏索引和标题标签:

df_1.to_csv('../data/ozone_copy.csv', encoding='utf-8', index=False, header=False)

查看数据目录:

!ls -l ../data/'''
total 1016
-rw-r--r--   1 donnemartin  staff  437903 Jul  7  2015 churn.csv
-rwxr-xr-x   1 donnemartin  staff   72050 Jul  7  2015 confusion_matrix.png
-rw-r--r--   1 donnemartin  staff    2902 Jul  7  2015 ozone.csv
-rw-r--r--   1 donnemartin  staff    3324 Apr  1 07:18 ozone_copy.csv
drwxr-xr-x  10 donnemartin  staff     340 Jul  7  2015 titanic
'''

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/96673.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息