熊猫类型.Series

时间:2021-06-07 07:09:27

标签: python python-3.x pandas

data = pandas.read_csv("weather_data.csv")

print(data)
print(data[data['day'] == 'Monday'])
print(type(data['day']))
print(type(data['day'] == 'Monday'))

输出

         day  temp condition
0     Monday    12     Sunny
1    Tuesday    14      Rain
2  Wednesday    15      Rain
3   Thursday    14    Cloudy
4     Friday    21     Sunny
5   Saturday    22     Sunny
6     Sunday    24     Sunny
      day  temp condition
0  Monday    12     Sunny
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>

我对最后的输出有点困惑。 data['day'] == 'Monday' 为什么会返回 panda.Series,为什么 data 的索引可以是 panda.Series

1 个答案:

答案 0 :(得分:0)

A DataFrame 是对齐的 Series 的集合,对齐意味着所有系列都以相同的方式标记。公共部分或标签称为 Index,它只不过是一个拉皮条的意甲。

使用 Series 的操作往往会返回一个 Serie 本身,它很容易是按元素进行比较,结果是一个大小相同且标签相同的布尔数组,称为 mask。< /p>

一些例子:

import pandas as pd
ds1 = pd.Series([2, 4, 6])
ds2 = pd.Series([1, 4, 7])

print(ds1)
print(type(ds1))

# out

0    2
1    4
2    6
dtype: int64
<class 'pandas.core.series.Series'>

玩面具:

dseq = ds1 == ds2

print(dseq)
print(type(dseq))

# out

0    False
1     True
2    False
dtype: bool
<class 'pandas.core.series.Series'>

这适用于所有比较:

print(ds1 > ds2)
print(ds1 < ds2)
print(ds1 >= ds2)

#out

0     True
1    False
2    False
dtype: bool
0    False
1    False
2     True
dtype: bool
0     True
1     True
2    False
dtype: bool
相关问题