我正在尝试按文档后面的极坐标数据框进行分组:
import polars as pl
df = pl.DataFrame(
{
"a": ["a", "b", "a", "b", "c"],
"b": [1, 2, 1, 3, 3],
"c": [5, 4, 3, 2, 1],
}
)
df.group_by("a").agg(pl.col("b").sum())
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
AttributeError: 'DataFrame' object has no attribute 'group_by'
Run Code Online (Sandbox Code Playgroud) import polars as pl
df = pl.DataFrame(
{
"X": [4, 2, 3, 4],
"Y": ["p", "p", "p", "p"],
"Z": ["b", "b", "b", "b"],
}
)
Run Code Online (Sandbox Code Playgroud)
我们知道pandas的等价物df.drop_duplicates()在python-polarsdf.unique()中
但是,每次执行查询时都会得到不同的结果?
print(df.unique())
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
3 "p" "b"
2 "p" "b"
4 "p" "b"
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
4 "p" "b"
2 "p" "b"
3 "p" "b"
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
2 "p" "b"
3 "p" "b"
4 "p" "b"
Run Code Online (Sandbox Code Playgroud)
这是故意的吗?背后的原因是什么?
当我运行这个玩具代码时
test = pd.DataFrame({'a': [1, 2, 3, 4]})
test['b'] = ''
for i in range(len(test)):
test['b'].loc[i] = [5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
我有一个警告
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self._setitem_single_block(indexer, value, name)
Run Code Online (Sandbox Code Playgroud)
loc但如果我按照这种方法使用
test = pd.DataFrame({'a': [1, 2, 3, 4]})
test['b'] = ''
for i in range(len(test)):
test.loc[i, 'b'] = [5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
ValueError: Must have equal len keys and …Run Code Online (Sandbox Code Playgroud) d={"given_age":"30","given_weight":"160","given_height":6}
Run Code Online (Sandbox Code Playgroud)
想要"given_"从每个键中删除,
for key,value in d.items():
new_key=re.sub(r'given_','',key)
if new_key!=key:
d[new_key]=d.pop(key)
Run Code Online (Sandbox Code Playgroud)
低于错误,我的目的是仅更改密钥,为什么它会抱怨?
RuntimeError: dictionary keys changed during iteration
Run Code Online (Sandbox Code Playgroud) 我想向 Azure OpenAI 的 ChatGPT 提问
我已经添加了帖子网址。我添加了参数“API_KEY”(从 Azure 门户复制的密钥),我添加了 json 正文。
我的访问被拒绝。我想密钥一定是正确的。有人可以发送示例工作 API 网址吗?
"error": {
"code": "401",
"message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."
}
}.
Run Code Online (Sandbox Code Playgroud) def no_six_in_range(start, end):
print(sum('6' not in str(i) for i in range(start, end + 1)))
no_six_in_range(6, 12)
Run Code Online (Sandbox Code Playgroud)
“sum() 函数返回一个数字,即可迭代对象中所有项目的总和。”
所以我的问题是,当 sum 将可迭代对象加在一起时,sum 将如何在这个方程中发挥作用。但是当您运行代码时,它给出的数字比给定的数字要小。如果有人可以向我解释这一点,我将不胜感激。
我有一个A包含True, False元素的 numpy 数组。我想打印所有包含False元素的索引。但是,我收到错误。我提出预期的输出:
import numpy as np
A=np.array([[False],
[False],
[ True],
[False],
[False]])
for i in range(0,len(A)):
if (A[i]==['False']):
print(i)
Run Code Online (Sandbox Code Playgroud)
错误是:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
if (A[i]==['False']):
Run Code Online (Sandbox Code Playgroud)
预期输出是:
[0,1,3,4]
Run Code Online (Sandbox Code Playgroud) 问题:
给定一个字符串 S,递归计算一个新字符串,其中原始字符串中相邻的相同字符用“*”彼此分隔。
例子:
Input = hello
Output = hel*lo
Run Code Online (Sandbox Code Playgroud)
我的代码:
def pairStar(s):
if len(s)<=1:
return s
if s[0]==s[1]:
return s[0]+"*"+pairStar(s[1:])
string=input()
print(pairStar(string))
My input: hello
My output: None
Run Code Online (Sandbox Code Playgroud)
请帮忙!输出显示“None”而不是“hel*lo”。
python ×7
python-3.x ×6
dataframe ×3
azure ×1
azure-openai ×1
dictionary ×1
list ×1
numpy ×1
pandas ×1
recursion ×1
sum ×1
unique ×1