sweets = {'cadbury': [180,90], 'candy': [190],
'milk chocolate': [150, 160], 'dark chocolate': [100],
'white chocolate': [180], 'ice cream': [122]}
Run Code Online (Sandbox Code Playgroud)
字典 sweets 有不同的键和列表值对。我希望得到您的帮助来查找值列表中数字的平均值,并返回具有最高平均值的键作为输出
我只是想了解 np.empty() ,我知道它创建了一个未初始化的数组,但我无法理解这意味着什么以及值来自哪里。欢迎任何帮助,提前致谢。
我有一个像这样的数据框:
Product_ID Quantity Year Quarter
1 100 2021 1
1 100 2021 2
1 50 2021 3
1 100 2021 4
1 100 2022 1
2 100 2021 1
2 100 2021 2
3 100 2021 1
3 100 2021 2
Run Code Online (Sandbox Code Playgroud)
我想获取每个 Product_ID 的过去三个月(不包括当月)的总和。
因此我尝试了这个:
df['Qty_Sum_3qrts'] = (df.groupby('Product_ID'['Quantity'].shift(1,fill_value=0)
.rolling(3).sum().reset_index(0,drop=True)
)
# Shifting 1, because I want to exclude the current row.
# Rolling 3, because I want to have the 3 'rows' before
# Grouping by, because I want to …
Run Code Online (Sandbox Code Playgroud) 我想将数据框中的列移动到最后一列,我尝试使用shift
. 但这并没有改变立场。
import pandas a pd
df = #input dataframe
df['x'] = df['x'].shift(axis=1)
Run Code Online (Sandbox Code Playgroud)
Error:
raise ValueError(f"No axis named {axis} for object type {cls.__name__}")
ValueError: No axis named 1 for object type Series
Run Code Online (Sandbox Code Playgroud)
还有其他选择吗?有人可以建议吗?
我想根据列的绝对值以升序或降序对极坐标数据框进行排序。Pandas
在Python中或者使用sorted
Python中的函数很容易做到。假设我想根据val
下面的数据框中的列进行排序。
import numpy as np\nnp.random.seed(42)\nimport polars as pl\n\ndf = pl.DataFrame({\n "name": ["one", "one", "one", "two", "two", "two"],\n "id": ["C", "A", "B", "B", "C", "C"],\n "val": np.random.randint(-10, 10, 6)\n })\n
Run Code Online (Sandbox Code Playgroud)\n返回:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x86 id \xe2\x94\x86 val \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 str \xe2\x94\x86 i32 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 one \xe2\x94\x86 C \xe2\x94\x86 -4 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 A \xe2\x94\x86 9 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 B \xe2\x94\x86 4 \xe2\x94\x82\n\xe2\x94\x82 two \xe2\x94\x86 B \xe2\x94\x86 …
Run Code Online (Sandbox Code Playgroud) 下面是我在 pandas 中的异常值检测代码。我正在滚动 15 个窗口,我想要的是滚动 5 个窗口,其中该窗口基于居中日期的星期几,即如果中心是星期一,则在星期一向后移动 2 个,在星期一向前移动 2 个。Rolling 对此没有任何支持。怎么做?
import pandas as pd
import numpy as np
np.random.seed(0)
dates = pd.date_range(start='2022-01-01', end='2023-12-31', freq='D')
prices1 = np.random.randint(10, 100, size=len(dates))
prices2 = np.random.randint(20, 120, size=len(dates)).astype(float)
data = {'Date': dates, 'Price1': prices1, 'Price2': prices2}
df = pd.DataFrame(data)
r = df.Price1.rolling(window=15, center=True)
price_up, price_low = r.mean() + 2 * r.std(), r.mean() - 2 * r.std()
mask_upper = df['Price1'] > price_up
mask_lower = df['Price1'] < price_low
df.loc[mask_upper, 'Price1'] = r.mean()
df.loc[mask_lower, 'Price1'] …
Run Code Online (Sandbox Code Playgroud) 有没有办法在不考虑顺序的情况下删除 pandas 中重复对的行?
删除前的数据框 --> 想要删除重复对(黄色)
删除重复后
示例数据:
df = pd.DataFrame({'a': [1,2,1,1,2,2],
'b': [2,1,3,4,3,4]
})
Run Code Online (Sandbox Code Playgroud) 我有一个包含两列的数据框
df = DataFrame.from_records([
{"time": 10, "amount": 200},
{"time": 70, "amount": 1000},
{"time": 10, "amount": 300},
{"time": 10, "amount": 100},
])
Run Code Online (Sandbox Code Playgroud)
我想要,给定一段时间80
ms,计算可能的最大数量,在这种情况下,输出应该是 1300,因为在此期间,可能的最大数量是 1300。
熊猫可以吗?我想过使用聚合,但我不知道如何使用它
我有以下数据框:
dt_datetime stage proc_val
2011-11-13 11:00 0 20
2011-11-13 11:10 0 21
2011-11-13 11:30 1 25
2011-11-13 11:40 2 22
2011-11-13 11:55 2 28
2011-11-13 12:00 2 29
Run Code Online (Sandbox Code Playgroud)
我需要添加一个名为的新列stage_duration
并获得以下结果:
dt_datetime stage proc_val stage_duration
2011-11-13 11:00 0 20 30
2011-11-13 11:10 0 21 30
2011-11-13 11:30 1 25 10
2011-11-13 11:40 2 22 20
2011-11-13 11:55 2 28 20
2011-11-13 12:00 2 29 20
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
这是我当前的代码片段,但它没有提供预期的结果。它应该计算具有相同阶段值的行之间的持续时间,然后获取每个阶段的累积持续时间,但事实并非如此。
df['stage_duration'] = df.groupby('stage')['dt_datetime'].diff().dt.total_seconds() / 60
df['stage_duration'] = df['stage_duration'].cumsum()
Run Code Online (Sandbox Code Playgroud)
更新:
如果数据帧包含多组阶段,该解决方案也应该有效,例如,请参阅从2011-11-13 11:00
和开始的阶段 …
我有数据框
Group Required stock
0 A 10 5
1 A 10 8
2 A 10 7
3 B 13 6
4 B 13 5
5 C 8 4
6 C 8 5
7 C 8 8
8 D 16 NaN
Run Code Online (Sandbox Code Playgroud)
这里所需的 A、B、C、D 是[10,13,8,16]
,我各自的库存在上面的表中提到。我需要标记所有需要移动的行以及需要移动的数量
输出应该是
Group Required stock to_move flag
0 A 10 5.0 5.0 yes
1 A 10 8.0 5.0 yes
2 A 10 7.0 0.0 no
3 B 13 6.0 6.0 yes
4 B 13 5.0 5.0 …
Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×7
dataframe ×4
numpy ×3
python-3.x ×2
aggregate ×1
dictionary ×1
duplicates ×1
list ×1
optimization ×1
shift ×1