本文共 1941 字,大约阅读时间需要 6 分钟。
的人们刚刚发布了熊猫的备忘单。 您可以在下载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 themdef 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) ]
翻译自:
转载地址:http://dtqwd.baihongyu.com/