标签:2.4 none frame bsp imp rename 判断 不能 div
import pandas as pd import numpy as np df = pd.DataFrame({"a": ["aa", np.NAN, np.NAN], "b": [3, np.NAN, 2]})
>>> df.a.isnull().any()
True
>>> df.a.isnull().all()
True
>>> 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
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
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
标签:2.4 none frame bsp imp rename 判断 不能 div
原文地址:https://www.cnblogs.com/spaceapp/p/10792491.html