pandas 初识
Posted spaceapp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas 初识相关的知识,希望对你有一定的参考价值。
Python Pandas 空值
- pandas 判断指定列是否(全部)为NaN(空值)
import pandas as pd import numpy as np df = pd.DataFrame({"a": ["aa", np.NAN, np.NAN], "b": [3, np.NAN, 2]})
-
- 判断某列是否有NaN
>>> df.a.isnull().any()
True
-
- 判断是否全部为 NAN
>>> df.a.isnull().all()
True
- NAN值替换:
- 不能使用,apply 去判断类型;DataFrame中np.nan 和 None 同为 isnull
>>> df.c.apply(None if x == np.nan else x) # 错误的方式,x的类型不是np.nan活着np.NAN, 任何类型都有可能。所以这种判断时可用 >>> df = df.where(df.notnull(), None) >>> print(df) >>> print(df.isnull()) a b c 0 aa None 1 None None None 2 None None None a b c 0 False False True 1 True True True 2 True True True
Pandas 使用 groupby 对数据分组进行迭代
import pandas as pd import numpy as np df = pd.DataFrame({‘key1‘: [‘a‘, ‘a‘, ‘b‘, ‘b‘, ‘a‘], ‘key2‘: [‘one‘, ‘two‘, ‘one‘, ‘two‘, ‘one‘], ‘data1‘: np.random.randn(5), ‘data2‘: np.random.randn(5)}) print(df) print("*********") class_key = ["key1", "key2"] # class_key = ["key1"] # 如果 class_key 是一个元素或者字符串,这class_ 为字符串 for class_, group in df.groupby(class_key): print("______________") print(class_) print(group)
结果:
key1 key2 data1 data2
0 a one 1.237276 -0.813727
1 a two 0.508972 -1.336699
2 b one -0.343635 0.714680
3 b two 2.433797 0.417454
4 a one 1.215114 2.646685
*********
______________
(‘a‘, ‘one‘)
key1 key2 data1 data2
0 a one 1.237276 -0.813727
4 a one 1.215114 2.646685
______________
(‘a‘, ‘two‘)
key1 key2 data1 data2
1 a two 0.508972 -1.336699
______________
(‘b‘, ‘one‘)
key1 key2 data1 data2
2 b one -0.343635 0.71468
______________
(‘b‘, ‘two‘)
key1 key2 data1 data2
3 b two 2.433797 0.417454
Pandas 修改列名
import pandas as pd
import numpy as np
a = pd.DataFrame({‘A‘: [1, 2, 3], ‘B‘: [4, 5, 6], ‘C‘: [7, 8, 9]})
print(a)
# a.colums = ["a", "b", "c"] # 错误的写法
a.rename(columns={‘A‘: ‘a‘, ‘C‘: ‘c‘}, inplace=True)
print(a)
结果:
A B C
0 1 4 7
1 2 5 8
2 3 6 9
a B c
0 1 4 7
1 2 5 8
2 3 6 9
以上是关于pandas 初识的主要内容,如果未能解决你的问题,请参考以下文章