<jupyter_start><jupyter_text>Series Series相当于数组numpy.array类似  index<jupyter_code>obj = pd.Series([4, 7, -5, 3])
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
obj
obj2 = pd.Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
obj2[1]
obj2['a']
obj2[['c', 'a', 'd']]<jupyter_output><empty_output><jupyter_text>筛选大于0<jupyter_code>obj2[obj2 > 0]<jupyter_output><empty_output><jupyter_text>运算<jupyter_code>obj2 * 2<jupyter_output><empty_output><jupyter_text>exp，高等数学里以自然常数e为底的指数函数<jupyter_code>import numpy as np
np.exp(obj2)<jupyter_output><empty_output><jupyter_text>创建字典<jupyter_code>sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon':16000, 'Utah': 5000}
type(sdata)<jupyter_output><empty_output><jupyter_text>还可以直接用现有的dict来创建series：<jupyter_code>obj3 = pd.Series(sdata)
obj3<jupyter_output><empty_output><jupyter_text>利用index排序 series中的index其实就是dict中排好序的keys。我们也可以传入一个自己想要的顺序：<jupyter_code>states = ['Utah', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index=states)
obj4<jupyter_output><empty_output><jupyter_text>缺失值 缺失值：pandas中的isnull和notnull函数可以用来检测缺失数据：<jupyter_code>states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index=states)
obj4
pd.isnull(obj4)
pd.notnull(obj4)
obj4.isnull()
df.isnull()#是缺失值返回True，否则返回False
df.isnull().sum()#返回每列包含的缺失值的个数
df.dropna()#直接删除含有缺失值的行
df.dropna(axis = 1)#直接删除含有缺失值的列
df.dropna(how = 'all')#只删除全是缺失值的行
df.dropna(thresh = 4)#保留至少有4个缺失值的行
df.dropna(subset = ['C'])#删除含有缺失值的特定的列<jupyter_output><empty_output><jupyter_text>name属性 serice自身和它的index都有一个叫name的属性，这个能和其他pandas的函数进行整合：<jupyter_code>obj4.name = 'population'
obj4.index.name = 'state'
obj4<jupyter_output><empty_output><jupyter_text>DataFrame DataFrame表示一个长方形表格，并包含排好序的列，每一列都可以是不同的数值类型（数字，字符串，布尔值）。DataFrame有行索引和列索引（row index, column index）；   可以类比Excel表格；     构建DataFrame 构建一个dataframe的方法，用一个dcit，dict里的值是list：  dataframe也会像series一样，自动给数据赋index, 而列则会按顺序排好。<jupyter_code>data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'], 
        'year': [2000, 2001, 2002, 2001, 2002, 2003], 
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
frame
#对于一个较大的DataFrame，用head方法会返回前5行（注：这个函数在数据分析中经常使用，用来查看表格里有什么东西）：
frame.head()
#交换列的顺序
pd.DataFrame(data, columns=['year', 'state', 'pop'])
#如果你导入一个不存在的列名，那么会显示为缺失数据：
frame2 = pd.DataFrame(data, columns=['year', 'state', 'pop', 'debt'], 
                      index=['one', 'two', 'three', 'four', 'five', 'six'])
frame2
#显示列名
frame2.columns<jupyter_output><empty_output><jupyter_text>提取某一列 注意：frame2[column]能应对任何列名，但frame2.column的情况下，列名必须是有效的python变量名才行。   返回的series有DataFrame种同样的index，而且name属性也是对应的。<jupyter_code>frame2['pop']
frame2.debt#？？？？<jupyter_output><empty_output><jupyter_text>提取某一行<jupyter_code>#对于行，要用在loc属性里用 位置或名字：
frame2.loc['three']<jupyter_output><empty_output><jupyter_text>列值赋值<jupyter_code>frame2['debt'] = 16.5
frame2
#np.arange()
frame2['debt'] = np.arange(6.)
frame2<jupyter_output><empty_output><jupyter_text>利用pandas的DataFrame实现数据聚合（group）<jupyter_code>import pandas as pd
from numpy.random import seed
from numpy.random import rand
from numpy.random import random_integers
import numpy as np

#seed() 方法改变随机数生成器的种子，可以在调用其他随机模块函数之前调用此函数。
seed(42)

df = pd.DataFrame({'Weather':['cold','hot','cold','hot','cold','hot','cold'],
                  'Food':['soup','soup','icecream','chololate','soup','icecream','chololate'],
                  'Price':10*rand(7),
                   'Number':random_integers(1,9,size=(7,))})
print(df)
weather_group = df.groupby('Weather')
print(weather_group)

i = 0

for name,group in weather_group:
    i = i+1
    print("Group"+str(i)+ name)
    print(group)

print("first group\n", weather_group.first())#输出组里第一行
print("last group\n", weather_group.last())#输出最后一行
print("mean group\n", weather_group.mean())#输出平均

wf_group = df.groupby(['Weather','Food'])
print("WF Groups:",wf_group.groups)<jupyter_output>Food  Number     Price Weather
0       soup       8  3.745401    cold
1       soup       5  9.507143     hot
2   icecream       4  7.319939    cold
3  chololate       8  5.986585     hot
4       soup       8  1.560186    cold
5   icecream       3  1.559945     hot
6  chololate       6  0.580836    cold
<pandas.core.groupby.DataFrameGroupBy object at 0x000002C9258D0BE0>
Group1cold
        Food  Number     Price Weather
0       soup       8  3.745401    cold
2   icecream       4  7.319939    cold
4       soup       8  1.560186    cold
6  chololate       6  0.580836    cold
Group2hot
        Food  Number     Price Weather
1       soup       5  9.507143     hot
3  chololate       8  5.986585     hot
5   icecream       3  1.559945     hot
first group
          Food  Number     Price
Weather                        
cold     soup       8  3.745401
hot      soup       5  9.507143
last group
               Food  Number     Price
Weather                             
cold     chololate   [...]
{'fraction_non_alphanumeric': 0.14373843306600864, 'fraction_numerical': 0.056343820686818834, 'mean_word_length': 4.5147392290249435, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '543667', 'license': 'MIT', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2294, 'n_words': 311, 'path': 'Python-Series&DataFrame.ipynb', 'repo_name': 'Yangyehong/python', 'size': '4863'}