我已经安装了所有必要的软件包:
pip install --upgrade snowflake-sqlalchemy
Run Code Online (Sandbox Code Playgroud)
我正在从雪花文档中运行这个测试代码:
from sqlalchemy import create_engine
engine = create_engine(
'snowflake://{user}:{password}@{account}/'.format(
user='<your_user_login_name>',
password='<your_password>',
account='<your_account_name>',
)
)
try:
connection = engine.connect()
results = connection.execute('select current_version()').fetchone()
print(results[0])
finally:
connection.close()
engine.dispose()
Run Code Online (Sandbox Code Playgroud)
我的输出应该是雪花版本,例如 1.48.0
但我收到错误:NoSuchModuleError:无法加载插件:sqlalchemy.dialects:snowflake
(我正在尝试在 Anaconda 中运行它)
我有这样的功能:
def highlight_otls(df):
return ['background-color: yellow']
Run Code Online (Sandbox Code Playgroud)
和这样的DataFrame:
price outlier
1.99 F,C
1.49 L,C
1.99 F
1.39 N
Run Code Online (Sandbox Code Playgroud)
我想要做的是根据另一列的这个条件突出显示我的df中的某个列:
data['outlier'].str.split(',').str.len() >= 2
Run Code Online (Sandbox Code Playgroud)
因此,如果列值df ['outlier']> = 2,我想突出显示相应的列df ['price'].(因此前两个价格应该在我上面的数据框中突出显示).
我尝试通过执行以下操作来执行此操作,这会给我一个错误:
data['price'].apply(lambda x: highlight_otls(x) if (x['outlier'].str.split(',').str.len()) >= 2, axis=1)
Run Code Online (Sandbox Code Playgroud)
如何以正确的方式做到这一点?
执行groupby后,有什么方法可以保留大数据帧的原始索引?我需要这样做的原因是因为我需要对我的原始df进行内部合并(在groupby之后)以重新获得那些丢失的列。索引值是执行合并回的唯一“唯一”列。有谁知道我怎么能做到这一点?
我的DataFrame很大。我的groupby看起来像这样:
df.groupby(['col1', 'col2'], ).agg({'col3': 'count'}).reset_index()
Run Code Online (Sandbox Code Playgroud)
这会将我的原始索引从我要保留的原始数据框中删除。
我有一个看起来像这样的大型DataFrame:df =
UPC Unit_Sales Price Price_Change Date
0 22 15 1.99 NaN 2017-10-10
1 22 7 2.19 True 2017-10-12
2 22 6 2.19 NaN 2017-10-13
3 22 7 1.99 True 2017-10-16
4 22 4 1.99 NaN 2017-10-17
5 35 15 3.99 NaN 2017-10-09
6 35 17 3.99 NaN 2017-10-11
7 35 5 4.29 True 2017-10-13
8 35 8 4.29 NaN 2017-10-15
9 35 2 4.29 NaN 2017-10-15
Run Code Online (Sandbox Code Playgroud)
基本上,我试图记录在接下来的7天价格变化后产品(UPC)的销售情况如何。我想创建一个新列['Reaction'],该列记录自价格更改之日起以及以后7天的单位销售额总和。请记住,有时UPC的价格变化超过2个,因此我希望每个价格变化的总和都不同。所以我想看看这个:
UPC Unit_Sales Price Price_Change Date Reaction
0 22 15 1.99 NaN 2017-10-10 …Run Code Online (Sandbox Code Playgroud) 我有这个:
df['new'] = df[['col1', 'col2']].pct_change(axis=1)
Run Code Online (Sandbox Code Playgroud)
我想要 col1 和 col2 中各行的百分比变化。但是我收到错误:
ValueError: Wrong number of items passed 2, placement implies 1
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有一个列(列 V),我曾经使用引擎 xlsxwriter 有条件地格式化另一列(列 U)。
所以我有这个:
# Light yellow fill with dark yellow text.
format1 = workbook.add_format({'bg_color': '#FFEB9C'})
# Light red fill with dark red text.
format2 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format('U2:U1000', {'type': 'formula',
'criteria': '=V2>25',
'format': format1})
worksheet.conditional_format('U2:U1000', {'type': 'formula',
'criteria': '=V2<-20',
'format': format2})
Run Code Online (Sandbox Code Playgroud)
所以现在在使用条件格式突出显示列 U 之后,我想删除列 V(但保持突出显示不变)。有没有办法在 xlsxwriter 中做到这一点?
我是 Pandas 的新手,我正在尝试弄清楚这种情况:我有一个包含两个产品的示例 DataFrame。df =
Product_Num Date Description Price
10 1-1-18 Fruit Snacks 2.99
10 1-2-18 Fruit Snacks 2.99
10 1-5-18 Fruit Snacks 1.99
10 1-8-18 Fruit Snacks 1.99
10 1-10-18 Fruit Snacks 2.99
45 1-1-18 Apples 2.99
45 1-3-18 Apples 2.99
45 1-5-18 Apples 2.99
45 1-9-18 Apples 1.49
45 1-10-18 Apples 1.49
45 1-13-18 Apples 1.49
45 1-15-18 Apples 2.99
Run Code Online (Sandbox Code Playgroud)
我还有另一个看起来像这样的小 DataFrame(显示相同产品的促销价格):df2=
Product_Num Price
10 1.99
45 1.49
Run Code Online (Sandbox Code Playgroud)
请注意,df2 不包含“日期”和“说明”列。我想要做的是使用 df1 中的数据从 df1 中删除所有促销价格(对于促销中的所有日期)。做这个的最好方式是什么?
所以,我想看看这个:
Product_Num Date Description …Run Code Online (Sandbox Code Playgroud) 我有一个非常大的 DataFrame,看起来像这个示例 df:
df =
col1 col2 col3
apple red 2.99
apple red 2.99
apple red 1.99
apple pink 1.99
apple pink 1.99
apple pink 2.99
... .... ...
pear green .99
pear green .99
pear green 1.29
Run Code Online (Sandbox Code Playgroud)
我按这样的 2 列分组:
g = df.groupby(['col1', 'col2'])
Run Code Online (Sandbox Code Playgroud)
现在我想选择 3 个随机组。所以我的预期输出是这样的:
col1 col2 col3
apple red 2.99
apple red 2.99
apple red 1.99
pear green .99
pear green .99
pear green 1.29
lemon yellow .99
lemon yellow .99
lemon yellow 1.99
Run Code Online (Sandbox Code Playgroud)
(假设以上三个组是来自 df …
我是 pandas 新手,我想在 pandas 数据框中创建一个新列。我想按一列进行分组,然后将其他两列划分在一起。
这完全有效:
df['new_col'] = (df.col2/df.col3)
Run Code Online (Sandbox Code Playgroud)
但是,当我对另一列进行分组时,我所拥有的不起作用:
df['new_col'] = df.groupby('col1')(df.col2/df.col3)
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何重写上面的代码?谢谢。
我一直试图弄清楚在申请groupby之后如何可以只返回第一个小组。
我的代码如下所示:
gb = df.groupby(['col1', 'col2', 'col3', 'col4'])['col5'].sum()
Run Code Online (Sandbox Code Playgroud)
我想要的是第一组输出。我一直在尝试get_group方法,但是它一直失败(也许是因为我正在按多列分组?)
这是我的输出示例:
col1 col2 col3 col4 'sum'
1 34 green 10 0.0
yellow 30 1.5
orange 20 1.1
2 89 green 10 3.0
yellow 5 0.0
orange 10 1.0
Run Code Online (Sandbox Code Playgroud)
我要退货的只是这样:
col1 col2 col3 col4 'sum'
1 34 green 10 0.0
yellow 30 1.5
orange 20 1.1
Run Code Online (Sandbox Code Playgroud)
(请注意,我刚刚在此处添加的“求和”列是为了清楚说明最后一列是什么,但pandas实际上并未命名该列)
我有这样的数据:
Group Provider
A ABC
A DEF
B DEF
B HIJ
Run Code Online (Sandbox Code Playgroud)
我想像这样转换数据:
Group ProviderList
A ABC, DEF
B DEF, HIJ
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用类似的方法,concat(select distinct...)但不确定这是否是最好的方法
SELECT distinct
group,
CONCAT(select distinct provider from data)
FROM data
GROUP BY 1
Run Code Online (Sandbox Code Playgroud) pandas ×9
python ×9
dataframe ×3
indexing ×1
postgresql ×1
snowflake-cloud-data-platform ×1
sql ×1
sqlalchemy ×1
xlsxwriter ×1