pivot_table我在与 的性能上挣扎groupby
一方面我有:
%time df.groupby(['INDEX', 'COLUMN']).agg({'VALUE':['sum','size']}).unstack(level='COLUMN')
CPU times: user 2.28 s, sys: 29.8 ms, total: 2.31 s
Wall time: 2.36 s
Run Code Online (Sandbox Code Playgroud)
另一方面我得到:
%time pd.pivot_table(df, index='INDEX', columns='COLUMN', values='VALUE', aggfunc=[len, np.sum], fill_value=0)
CPU times: user 1min 51s, sys: 1.57 s, total: 1min 53s
Wall time: 1min 54s
Run Code Online (Sandbox Code Playgroud)
这些本质上是相同的,但我得到了 60 倍的性能差异。这是为什么?
我的样本中有 800k 行,其中大约 400k 是唯一的INDEX,并且COLUMN有 16 个唯一值。
我有一个数据框:
lft rel rgt num
0 t3 r3 z2 3
1 t1 r3 x1 9
2 x2 r3 t2 8
3 x4 r1 t2 4
4 t1 r1 z3 1
5 x1 r1 t2 2
6 x2 r2 t4 4
7 z3 r2 t4 5
8 t4 r3 x3 4
9 z1 r2 t3 4
Run Code Online (Sandbox Code Playgroud)
以及参考词典:
replacement_dict = {
'X1' : ['x1', 'x2', 'x3', 'x4'],
'Y1' : ['y1', 'y2'],
'Z1' : ['z1', 'z2', 'z3']
}
Run Code Online (Sandbox Code Playgroud)
我的目标是将所有出现的 'X1' 替换replacement_dict['X1'],然后计算行的分组总和 …
我按照本教程迈出了深度学习的第一步,一切都很顺利,直到我需要在 Jupyter Notebook 中训练网络。我几乎尝试了所有方法,但总是收到此错误
The kernel appears to have died. It will restart automatically.
Run Code Online (Sandbox Code Playgroud)
当我检查终端时我可以看到这个
[I 18:32:24.897 NotebookApp] Adapting to protocol v5.1 for kernel 0d2f57af-46f5-419c-8c8e-9676c14dd9e3
2019-03-09 18:33:12.906756: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-03-09 18:33:12.907661: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 4. Tune using inter_op_parallelism_threads for best performance.
OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
OMP: Hint: …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
class Person:
def __init__(self, name, last_name, age):
self.name = name
self.last_name = last_name
self.age = age
class Student(Person):
def __init__(self, name, last_name, age, indexNr, notes):
super().__init__(name, last_name, age)
self.indexNr = indexNr
self.notes = notes
class Employee(Person):
def __init__(self, name, last_name, age, salary, position):
super().__init__(name, last_name, age)
self.salary = salary
self.position = position
class WorkingStudent(Student, Employee):
def __init__(self, name, last_name, age, indexNr, notes, salary, position):
Student.__init__(name, last_name, age, indexNr, notes)
Employee.__init__(name, last_name, age, salary, position)
Run Code Online (Sandbox Code Playgroud)
我想创建一个像这样的WorkingStudent实例:
ws = WorkingStudent("john", "brown", …Run Code Online (Sandbox Code Playgroud) 假设我有一个包含概率列的数据框。现在我创建一个映射函数,如果概率大于阈值,则返回 1,否则返回 0。现在的问题是,我想通过将阈值作为函数的参数来指定阈值,然后将其映射到熊猫数据框。
以下面的代码为例:
def partition(x,threshold):
if x<threshold:
return 0
else:
return 1
df = pd.DataFrame({'probability':[0.2,0.8,0.4,0.95]})
df2 = df.map(partition)
Run Code Online (Sandbox Code Playgroud)
我的问题是,最后一行如何工作,即如何在地图函数内传递阈值?
我正在尝试构建一个可以讨论 pdf 的聊天机器人,并且我让它与内存一起使用ConversationBufferMemory,ConversationalRetrievalChain就像本例中一样。https://python.langchain.com/en/latest/modules/chains/index_examples/chat_vector_db.html
现在我试图给人工智能一些特殊的指令,让它像海盗一样说话(只是为了测试它是否收到指令)。我认为这应该是一个SystemMessage,或者带有提示模板的东西?
我已经尝试了我发现的所有内容,但文档中的所有示例都是针对的ConversationChain,但我最终遇到了问题。到目前为止,唯一没有出现任何错误的是:
template = """Given the following conversation respond to the best of your ability in a pirate voice and end every sentence with Ay Ay Matey
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
PROMPT = PromptTemplate(
input_variables=["chat_history", "question"], template=template
)
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True, output_key='answer')
qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), PROMPT, memory=memory, return_source_documents=True)
Run Code Online (Sandbox Code Playgroud)
它仍然对结果没有任何影响,所以我不知道它是否有任何作用。我还认为这是错误的方法,我应该使用SystemMessages (也许在内存上,而不是在 qa 上),但我从文档中尝试的任何操作都不起作用,我不知道该怎么做。
我有一个包含非常大的整数的列表,我想将其转换为具有特定数据类型的 pandas 列。举个例子,如果列表包含2**31,超出了 int32 dtype 的限制,则将其转换为 dtype int32 会引发溢出错误,这让我知道要使用另一个 dtype 或提前以其他方式处理该数字。
import pandas as pd\npd.Series([2**31], dtype=\'int32\')\n\n# OverflowError: Python int too large to convert to C long\nRun Code Online (Sandbox Code Playgroud)\n但是,如果一个数字很大,但在 dtype 限制内(即2**31-1),并且向其中添加了一些数字,导致值超出了 dtype 限制,则执行该操作时不会出现任何错误,而不是出现 OverflowError,但值现在已反转,成为该列的完全错误的数字。
pd.Series([2**31-1], dtype=\'int32\') + 1\n\n0 -2147483648\ndtype: int32\nRun Code Online (Sandbox Code Playgroud)\n为什么会发生这种情况?为什么 \xe2\x80\x99 不会像第一种情况那样引发错误?
\n附言。我在 Python 3.12.0 上使用 pandas 2.1.1 和 numpy 1.26.0。
\n这是我的数据框:
df = pd.DataFrame({'a': [20, 21, 333, 444], 'b': [20, 20, 20, 20]})
Run Code Online (Sandbox Code Playgroud)
c我想使用这个掩码创建列:
mask = (df.a >= df.b)
Run Code Online (Sandbox Code Playgroud)
我想获取满足此条件的最后一行并创建列c。我想要的输出如下所示:
a b c
0 20 20 NaN
1 21 20 NaN
2 333 20 NaN
3 444 20 x
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的代码,但它不起作用:
df.loc[mask.cumsum().gt(1) & mask, 'c'] = 'x'
Run Code Online (Sandbox Code Playgroud) 我在一个python教程网站上找到了以下代码:
from nltk.tag import StanfordNERTagger
stanford_classifier = open("english.all.3class.distsim.crf.ser.gz")
stanford_ner_path = open("stanford-ner.jar")
st = StanfordNERTagger(stanford_classifier, stanford_ner_path)
Run Code Online (Sandbox Code Playgroud)
错误如下:
from nltk.tag import StanfordNERTagger
stanford_classifier = open("english.all.3class.distsim.crf.ser.gz")
stanford_ner_path = open("stanford-ner.jar")
st = StanfordNERTagger(stanford_classifier, stanford_ner_path)
Run Code Online (Sandbox Code Playgroud) 给定下面的数据框,如果行按名字和姓氏分组,我如何为类型列表中没有每种类型的行找到并创建新行。
因此,在下面的示例中,将为 Bob Jack 创建原始数据帧中缺少的两行新行:一行的类型为“DA”,另一行的类型为“FA”,值列可以设置为 0。
data = {
'First Name': ['Alice', 'Alice', 'Alice', 'Alice', 'Bob', 'Bob'],
'Last Name': ['Johnson', 'Johnson', 'Johnson', 'Johnson', 'Jack', 'Jack'],
'Type': ['CA', 'DA', 'FA', 'GCA', 'CA', 'GCA'],
'Value': [25, 30, 35, 40, 50, 37]
}
types = ['CA', 'DA', 'FA', 'GCA']
df = pd.DataFrame(data)
Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×6
dataframe ×4
group-by ×3
python-3.x ×3
pivot-table ×2
apply ×1
duplicates ×1
file-io ×1
indexing ×1
long-integer ×1
mnist ×1
numpy ×1
oop ×1
openai-api ×1
py-langchain ×1
stanford-nlp ×1
tensorflow ×1
typeerror ×1