import pandas as pdimport numpy as npdf = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),'B': 'one one two three two two one three'.split(),'C': np.arange(8), 'D': np.arange(8) * 2})print(df)# A B C D# 0 foo one 0 0# 1 bar one 1 2# 2 foo two 2 4# 3 bar three 3 6# 4 foo two 4 8# 5 bar two 5 10# 6 foo one 6 12# 7 foo three 7 14
print(df.loc[df['A'] == 'foo'])
产生
A B C D0 foo one 0 02 foo two 2 44 foo two 4 86 foo one 6 127 foo three 7 14
如果您有多个要包含的值,请将它们放入列出(或更一般地,任何可迭代的)并使用isin:
print(df.loc[df['B'].isin(['one','three'])])
产生
A B C D0 foo one 0 01 bar one 1 23 bar three 3 66 foo one 6 127 foo three 7 14
但是,请注意,如果您希望多次执行此操作,则更有效的方法是先做一个索引,然后使用df.loc:
df = df.set_index(['B'])print(df.loc['one'])
产生
A C DBone foo 0 0one bar 1 2one foo 6 12
或者,要包含索引中的多个值,请使用df.index.isin:
df.loc[df.index.isin(['one','two'])]
产生
A C DBone foo 0 0one bar 1 2two foo 2 4two foo 4 8two bar 5 10one foo 6 12
import pandas as pddf = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),'B': 'one one two three two two one three'.split()})print("Original dataframe:")print(df)
b_is_two_dataframe = pd.DataFrame(df.groupby('B').get_group('two').reset_index()).drop('index', axis = 1)#NOTE: the final drop is to remove the extra index column returned by groupby objectprint('Sub dataframe where B is two:')print(b_is_two_dataframe)
运行它可以提供:
Original dataframe:A B0 foo one1 bar one2 foo two3 bar three4 foo two5 bar two6 foo one7 foo threeSub dataframe where B is two:A B0 foo two1 foo two2 bar two
In [76]: df.iloc[np.where(df.A.values=='foo')]Out[76]:A B C D0 foo one 0 02 foo two 2 44 foo two 4 86 foo one 6 127 foo three 7 14
定时比较:
In [68]: %timeit df.iloc[np.where(df.A.values=='foo')] # fastest1000 loops, best of 3: 380 µs per loop
In [69]: %timeit df.loc[df['A'] == 'foo']1000 loops, best of 3: 745 µs per loop
In [71]: %timeit df.loc[df['A'].isin(['foo'])]1000 loops, best of 3: 562 µs per loop
In [72]: %timeit df[df.A=='foo']1000 loops, best of 3: 796 µs per loop
In [74]: %timeit df.query('(A=="foo")') # slowest1000 loops, best of 3: 1.71 ms per loop
import pandas as pd, numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),'B': 'one one two three two two one three'.split(),'C': np.arange(8), 'D': np.arange(8) * 2})
pandas.notna(df["colume_name"]) == True # Not NaNdf['colume_name'].str.contains("text") # Search for "text"df['colume_name'].str.lower().str.contains("text") # Search for "text", after converting to lowercase
s=datetime.datetime.now()
my_dict={}
for i, my_key in enumerate(df['some_column'].values):if i%100==0:print(i) # to see the progressif my_key not in my_dict.keys():my_dict[my_key]={}my_dict[my_key]['values']=[df.iloc[i]['another_column']]else:my_dict[my_key]['values'].append(df.iloc[i]['another_column'])
e=datetime.datetime.now()
print('operation took '+str(e-s)+' seconds')```