- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在创建名为 payment_types_Owned
的维度表时遇到问题列出客户拥有的产品数量、余额以及每次付款的限额。目前,我有一个看起来像这样的表:
cust_id 支付类型 X 拥有 支付类型 Y 拥有 支付类型 Z 拥有 已用信用_X 限制_X 已用信用_Y 限制_Y 已用信用_Z 限制_Z
0 人_A 1 3 4 300 700 700 800 400 900
1 人_B 2 1 3 400 600 100 150 400 500
2 人_C 2 4 4 500 600 700 800 100 500
我想要的输出:
cust_id 变量值 Credit Used Limit
0 Person_A_key 付款类型 X 1 300 700
1 Person_A_key 付款类型 Y 3 700 800
2 Person_A_key 付款类型 Z 4 400 900
3 Person_B_key 支付类型 X 2 400 600
4 Person_B_key 付款类型 Y 1 100 150
5 Person_B_key 付款类型 Z 3 400 500
假设我已经有另外 2 个维度表可以捕获以下信息:
Customer Dimension Table
- 包含 cust_id 主键 Product Dimension Table
- 包含唯一的产品主键 pd.melt()
,我得到以下,但它只是部分解决我的问题:
最佳答案
使用 wide_to_long
,但首先必须使用 Series.str.replace
与第一组Payment Type
列:
df.columns = df.columns.str.replace(' owned', '').str.replace('Payment Type ', 'Payment Type_')
print (df)
cust_id Payment Type_X Payment Type_Y Payment Type_Z Credit Used_X \
0 Person_A 1 3 4 300
1 Person_B 2 1 3 400
2 Person_C 2 4 4 500
Limit_X Credit Used_Y Limit_Y Credit Used_Z Limit_Z
0 700 700 800 400 900
1 600 100 150 400 500
2 600 700 800 100 500
df1 = pd.wide_to_long(df, stubnames=['Payment Type','Credit Used', 'Limit'],
i='cust_id',
j='variable',
sep='_',
suffix='\w+').sort_index(level=0).reset_index()
variable
列并按字典重命名列:
df1 = (df1.assign(variable='Payment Type ' + df1['variable'])
.rename(columns={'Payment Type':'value'}))
print(df1)
cust_id variable value Credit Used Limit
0 Person_A Payment Type X 1 300 700
1 Person_A Payment Type Y 3 700 800
2 Person_A Payment Type Z 4 400 900
3 Person_B Payment Type X 2 400 600
4 Person_B Payment Type Y 1 100 150
5 Person_B Payment Type Z 3 400 500
6 Person_C Payment Type X 2 500 600
7 Person_C Payment Type Y 4 700 800
8 Person_C Payment Type Z 4 100 500
关于python-3.x - 如何在多列中使用 pd.melt()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169347/
我有一个这样的DataFrame >>> df = pd.DataFrame([[1,1,2,3,4,5,6],[2,7,8,9,10,11,12]], c
很抱歉,如果这是一个重复的问题,但我找不到问题的答案。 我有一个当前的数据框: 深度 Year Counts Depth.1 Year.1 Counts.1 Depth.2 Year.2 Counts
我正在尝试更改数据集的结构 目前有: RE id Country 0 1 2 ... n 1001 CN,TH CN TH nan ...
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: Error: could not find function … in R 我找到了对函数 melt() 的各种引用
这个问题已经有答案了: How to reshape data from long to wide format (14 个回答) 已关闭 7 年前。 我有像这样的巨大数据框: SN = c(1:10
我有一个小问题,在概念上似乎很简单,但我找不到解决方法... 假设我有一个 data.frame df2,其中有一列列出汽车品牌,另一列包含每个品牌的所有型号,用“,”分隔。我已经获得了 df2 聚合
我想融合 dataframe 以便在一列中我有日期,然后我有用户名作为变量,最后是值。 我收到此错误: Error in as.Date.numeric(value) : 'origin' must
我有几个不同长度的视频剪辑。我想在这些剪辑上混音。 我用这个: melt original.mp4 -audio-track preview.mp3 -transition mix 如果 previe
我目前正在进行一项测试,其中我有不同的区域和一些相关的统计数据,以及位于这些区域的以逗号分隔的基因列表。此列表的数量是可变的,并且可能不包含任何内容 ("NA")。 我怎样才能“融化”这个数据框:
我有一个像这样的宽格式数据集 Index Country Variable 2000 2001 2002 2003 2004 2005 0 Argentina var1
我只想在 pandas 中使用 melt 函数,但我总是遇到同样的错误。 只需键入文档提供的示例: cheese = pd.DataFrame({'first' : ['John', 'Mary'],
在pandas标签上,我经常看到用户询问关于熊猫融化的问题。我将尝试一个规范的Q&A(自我回答)与这个主题。。我要澄清的是:。我看到了一些关于融化的更热门的问题,比如:。使用Pandas将列转换为行:
我有一个如下所示的数据框: Country Person A B C 0 USA Bob 200 300 400 1 Canada Alice 100
例如这是我的数据框 df = pd.DataFrame([['Bob', 'lunch', 70], ['Bob', 'dinner', 160],
我有下面的 SQL 表,我们称之为teams_score,它显示了一场比赛的 2 名球员的球队及其得分。每次他们以 Player1 或 Player2 出现在团队中时,我都需要观察每个球员的得分。我认
我有一个如下所示的数据框: 名称 值 1 值 2 A 100 101 A 100 102 A 100 103 B 200 201 B 200 202 B 200 203 C 300 301 C 30
假设我有以下 data.frame: foo foo CONTACT_DATE ISSUE ISSUE_COUNT RANK 1 2015-09-15 abc 1000
例如,我有以下 map : {'df1': Jan Feb Mar 1 3 5 2 4 6 '
问题 我发现自己在使用 reshape2::melt获得单个“长”data.frame来自分层 list的 data.frame对象。但是,结果的列名称具有标记为“L1”、“L2”等的列表层次结构级别
我正在尝试融化我的 Pandas 数据框,但我不确定如何正确分配变量。我查看了堆栈上的其他示例,但似乎找不到与此匹配的变体。我的数据框 (df1) 如下所示: [IN]: df1 [OUT]:
我是一名优秀的程序员,十分优秀!