1.isin函数的应用
isin函数用于筛选数据框中某一列的值是否属于给定的序列
2.drop函数
使用drop,默认地,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据
如果设置参数inplace=True,则会直接改变原有的df中的数据
3.截取函数
使用截取函数去掉尾部一个字符:[:-1]
使用截取函数去掉头部一个字符:[1:]
使用截取函数去掉头部尾部一个字符:[1:-1]
import pandas as pd
import numpy as np
# 1. isin函数的应用
# 告诉你没有isnotin,它的反函数就是在前面加上~
df = pd.DataFrame(np.random.randn(4, 4), columns=['A', 'B', 'C', 'D'])
df['E'] = ['a', 'a', 'c', 'b']
df.D = [0, 1, 0, 2]
print(df[df.E.isin(['a', 'd']) & df.D.isin([0, ])])
print(df)
print(df[~df.E.isin(['a', 'd']) & df.D.isin([0, ])])
# 2. drop函数 使用drop,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据
df = pd.DataFrame(np.arange(12).reshape(3, 4), columns=['A', 'B', 'C', 'D'])
print('--原df--')
print(df)
df.drop(['B', 'C'], axis=1) # 删除列
df.drop(columns=['B', 'C']) # 删除列
df.drop([0, 1], inplace=True) # 按索引删除
print(df)
df.dropna() # 将所有含有nan项的row删除
# 3. 截取函数
string = "Hello world!"
string1 = string[:-1] # 去掉尾部一个字符
string2 = string[1:-1] # 去掉头尾各一个字符
string3 = string[1:] # 去掉头部一个字符
print(string1)
print(string2)
print(string3)
print("23060401^欧菲".find("23060401") > 0)
print('23060401^欧菲' in '23060401^欧菲')
输出结果:
A B C D E
0 -0.398638 -0.443538 -1.102833 0 a
A B C D E
0 -0.398638 -0.443538 -1.102833 0 a
1 1.275424 -0.223727 0.162261 1 a
2 -0.592653 1.249685 -0.030294 0 c
3 1.791002 -1.481358 0.544365 2 b
A B C D E
2 -0.592653 1.249685 -0.030294 0 c
--原df--
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
A B C D
2 8 9 10 11
Hello world
ello world
ello world!
False
True
17万+

被折叠的 条评论
为什么被折叠?



