可以说我有三个不同的MySQL表:
表products:
id | name
1 Product A
2 Product B
Run Code Online (Sandbox Code Playgroud)
表partners:
id | name
1 Partner A
2 Partner B
Run Code Online (Sandbox Code Playgroud)
表sales:
partners_id | products_id
1 2
2 5
1 5
1 3
1 4
1 5
2 2
2 4
2 3
1 1
Run Code Online (Sandbox Code Playgroud)
我想得到一个表格,其中包含行和产品中的合作伙伴作为列.到目前为止,我能够得到这样的输出:
name | name | COUNT( * )
Partner A Product A 1
Partner A Product B 1
Partner A Product C 1
Partner A Product D 1
Partner …Run Code Online (Sandbox Code Playgroud) 我有一个像元组的列表
data = [
('r1', 'c1', avg11, stdev11),
('r1', 'c2', avg12, stdev12),
('r2', 'c1', avg21, stdev21),
('r2', 'c2', avg22, stdev22)
]
Run Code Online (Sandbox Code Playgroud)
我想将它们放入一个pandas DataFrame中,其中第一列命名的行和第二列命名的列.似乎照顾行名称的方式是类似pandas.DataFrame([x[1:] for x in data], index = [x[0] for x in data])但是如何处理列以获得2x2矩阵(前一组的输出是3x4)?是否有更智能的方式来处理行标签,而不是明确省略它们?
编辑似乎我需要2个DataFrames - 一个用于平均值,一个用于标准偏差,这是正确的吗?或者我可以在每个"单元格"中存储值列表吗?
我是SQL新手.
我有这样一张桌子:
ID | TeamID | UserID | ElementID | PhaseID | Effort
-----------------------------------------------------
1 | 1 | 1 | 3 | 5 | 6.74
2 | 1 | 1 | 3 | 6 | 8.25
3 | 1 | 1 | 4 | 1 | 2.23
4 | 1 | 1 | 4 | 5 | 6.8
5 | 1 | 1 | 4 | 6 | 1.5
Run Code Online (Sandbox Code Playgroud)
我被告知要获得这样的数据
ElementID | PhaseID1 | PhaseID5 | PhaseID6
--------------------------------------------
3 | …Run Code Online (Sandbox Code Playgroud) 官方Laravel文档具有以下sync()功能:
$user->roles()->sync( array( 1, 2, 3 ) );
Run Code Online (Sandbox Code Playgroud)
您还可以将其他数据透视表值与给定ID相关联:
$user->roles()->sync( array( 1 => array( 'expires' => true ) ) );
Run Code Online (Sandbox Code Playgroud)
在后一示例中,仅添加了一个枢轴行.我不明白的是,如果要同步多行,如何关联其他数据透视表记录?
提前致谢.
我开始使用Spark DataFrames,我需要能够透过数据来创建多列的1列中的多列.在Scalding中有内置的功能,我相信Python中的Pandas,但我找不到任何新的Spark Dataframe.
我假设我可以编写某种类型的自定义函数,但是我甚至不确定如何启动,特别是因为我是Spark的新手.我有人知道如何使用内置功能或如何在Scala中编写内容的建议,非常感谢.
我无法弄清楚如何在python中使用Pandas进行"反向融化".这是我的起始数据
import pandas as pd
from StringIO import StringIO
origin = pd.read_table(StringIO('''label type value
x a 1
x b 2
x c 3
y a 4
y b 5
y c 6
z a 7
z b 8
z c 9'''))
origin
Out[5]:
label type value
0 x a 1
1 x b 2
2 x c 3
3 y a 4
4 y b 5
5 y c 6
6 z a 7
7 z b 8
8 z …Run Code Online (Sandbox Code Playgroud) 我试图根据同一个表格中的ProductID在新列中获取"现金","检查"和"信用卡"总计.
表 - 付款
+-----------+------------+---------------+--------+
| ProductID | SaleDate | PaymentMethod | Amount |
+-----------+------------+---------------+--------+
| 3 | 2012-02-10 | Cash | 10 |
| 3 | 2012-02-10 | Cash | 10 |
| 3 | 2012-02-10 | Check | 15 |
| 3 | 2012-02-10 | Credit Card | 25 |
| 4 | 2012-02-10 | Cash | 5 |
| 4 | 2012-02-10 | Check | 6 |
| 4 | 2012-02-10 | Credit Card | 7 | …Run Code Online (Sandbox Code Playgroud) 我试图在PIVOT函数中使用0(零)输出转换(null)值但没有成功.
下面是表格和我试过的语法"
SELECT
CLASS,
[AZ],
[CA],
[TX]
FROM #TEMP
PIVOT (SUM(DATA)
FOR STATE IN ([AZ], [CA], [TX])) AS PVT
ORDER BY CLASS
CLASS AZ CA TX
RICE 10 4 (null)
COIN 30 3 2
VEGIE (null) (null) 9
Run Code Online (Sandbox Code Playgroud)
我试图使用ISNULL但没有奏效.
PIVOT SUM(ISNULL(DATA,0)) AS QTY
Run Code Online (Sandbox Code Playgroud)
有人可以查看它的语法错误吗?非常感谢!
我想在熊猫上运行一个轴DataFrame,索引是两列,而不是一列.例如,年份的一个字段,月份的一个字段,显示"项目1"和"项目2"的"项目"字段和带有数值的"值"字段.我希望索引是年+月.
我设法让这个工作的唯一方法是将两个字段合并为一个,然后再将它们分开.有没有更好的办法?
下面复制的最小代码.非常感谢!
PS是的,我知道关键字'pivot'和'multi-index'还有其他问题,但我不明白他们是否/如何帮助我解决这个问题.
import pandas as pd
import numpy as np
df= pd.DataFrame()
month = np.arange(1, 13)
values1 = np.random.randint(0, 100, 12)
values2 = np.random.randint(200, 300, 12)
df['month'] = np.hstack((month, month))
df['year'] = 2004
df['value'] = np.hstack((values1, values2))
df['item'] = np.hstack((np.repeat('item 1', 12), np.repeat('item 2', 12)))
# This doesn't work:
# ValueError: Wrong number of items passed 24, placement implies 2
# mypiv = df.pivot(['year', 'month'], 'item', 'value')
# This doesn't work, either:
# df.set_index(['year', 'month'], inplace=True) …Run Code Online (Sandbox Code Playgroud) 我有一个有趣的难题,我认为可以用纯粹的SQL解决.我有类似于以下的表:
responses:
user_id | question_id | body
----------------------------
1 | 1 | Yes
2 | 1 | Yes
1 | 2 | Yes
2 | 2 | No
1 | 3 | No
2 | 3 | No
questions:
id | body
-------------------------
1 | Do you like apples?
2 | Do you like oranges?
3 | Do you like carrots?
Run Code Online (Sandbox Code Playgroud)
我想得到以下输出
user_id | Do you like apples? | Do you like oranges? | Do you like carrots?
---------------------------------------------------------------------------
1 …Run Code Online (Sandbox Code Playgroud) pivot ×10
pandas ×3
python ×3
sql ×3
mysql ×2
sql-server ×2
apache-spark ×1
dataframe ×1
eloquent ×1
group-by ×1
laravel ×1
left-join ×1
melt ×1
multi-index ×1
php ×1
postgresql ×1
python-2.7 ×1
reshape ×1
scala ×1
select ×1
sync ×1
t-sql ×1