pandas针对dataframe各种操作技巧集合:

python/numpy/pandas数据操作知识与技巧

filtering:

一般地,使用df.column > xx将会产生一个只有boolean值的series,以该series作为dataframe的选择器(index/slicing)将直接选中该series中所有value为true的记录。

df[df.salt>60]  # 返回所有salt大于60的行
df[(df.salt>50)&(df.eggs < 300)] # 返回salt大于50并且eggs小于300的行
print(df2.loc[:,df2.all()]) # 打印不含0值的所有列(所有行)
print(df2.loc[:,df2.any()]) #打印所有含非0值的所有列(所有行)
print(df2.loc[:,df2.isnull().any()]) #打印所有包含一个NaN值的列(所有行)
print(df2.loc[:,df2.notnull().all()]) #打印所有满值列(不含空值)(所有行)
df.dropna(how='any') # 将任何含有nan的行删除

filter过滤并赋值

# Create the boolean array: too_close
too_close = election['margin']<1
# Assign np.nan to the 'winner' column where the results were too close to call
election.loc[too_close,'winner'] = np.nan
# 等价于以下,需要注意的是[column][row]和loc[row,column]是反过来的哦!!!!
election['winner'][too_close] = np.nan

 

 

dict(list(zip()))创建DataFrame

 

就地修改某列数据类型为数值型,无法parse成功的则设为NaN

df['salt'] = pd.to_numeric(df['salt'],errors='coerce')

 setting index with combined column:列组合作为index(比如股票名称+日期)

获取df.loc['rowname','colname']==df.iloc[x,y]中的x和y

x = election.index.get_loc('Bedford') # 行名称为Bedford
y = election.columns.get_loc('winner') #列名称为winner
# 这时:
election.loc['Bedford','winner'] == election.iloc[x,y]
election.winner[too_close] = np.nan

 

 

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-02-25
  • 2021-11-19
  • 2021-12-18
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2022-02-14
  • 2021-08-04
  • 2021-04-26
  • 2021-04-06
  • 2021-12-13
  • 2021-12-26
  • 2021-07-07
相关资源
相似解决方案