熊猫作弊表(通过yhat)

pandas cheat sheetThe folks over at
yhat just released a cheat sheet for pandas.  You can download the cheat sheet in PDF for here.

yhat上的人们刚刚发布了熊猫的备忘单。 您可以在此处下载PDF备忘单。

There’s a couple important functions that I use all the time missing from their cheat sheet (actually….there are a lot of things missing, but its a great starter cheat sheet).

我的备忘单一直缺少一些重要的功能(实际上……有很多东西遗失,但它是一个很棒的入门备忘单)。

A few things that I use all the time with pandas dataframes that are worth collecting in one place are provided below.

下面提供了我一直在使用的一些值得在一个地方收集的pandas数据框的注意事项。

Renaming columns in a pandas dataframe:

重命名熊猫数据框中的列:

df.rename(columns={'col1': 'Column_1', 'col2': 'Column_2'}, inplace=True)

Iterating over a pandas dataframe:

遍历熊猫数据框:

for index, row in df.iterrows():
 * DO STUFF

Splitting pandas dataframe into chunks:

将pandas数据框拆分为多个块:

The function plus the function call will split a pandas dataframe (or list for that matter) into NUM_CHUNKS chunks. I use this often when working with the multiprocessing libary.

函数加上函数调用会将熊猫数据框(或相关列表)拆分为NUM_CHUNKS个块。 在使用多处理库时,我经常使用它。

# This function creates chunks and returns them
def chunkify(lst,n):
    return [ lst[i::n] for i in xrange(n) ]

chunks = chunkify(df, NUMCHUNKS)

Accessing the value of a specific cell:

访问特定单元格的值:

This will give you the value of the last row’s “COLUMN” cell.  This may not be the ‘best’ way to do it, but it gets the value

这将为您提供最后一行的“ COLUMN”单元格的值。 这可能不是实现此目标的“最佳”方法,但它可以带来价值

df.COLUMN.tail(1).iloc[0]

Getting rows matching a condition:

获取符合条件的行:

The below will get all rows in a pandas dataframe that match the criteria.  In addition to finding equality, you can do all the logical operators.

下面将获取熊猫数据框中符合条件的所有行。 除了寻找相等性,您还可以执行所有逻辑运算符。

df[df.COLUMN == Criteria]

Getting rows matching multiple conditions:

获取符合多个条件的行:

This gets rows that match a criteria in COLUMN1 and those that match another criteria in COLUMN2

这将获得与COLUMN1中的条件匹配的行以及与COLUMN2中的另一个条件匹配的行

 df[(df.COLUMN1 == Criteria) & (df.COLUMN2 == Criteria_2) ]

翻译自: https://www.pybloggers.com/2016/11/pandas-cheat-sheet-via-yhat/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值