You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas技术实现:将横向多产品账户数据转置为纵向格式并重复账户关联信息

Reshape Pandas DataFrame for Salesforce Upload (Wide to Long Format)

This is a classic wide-to-long data reshaping task, and pandas has a built-in function melt() that makes this super straightforward. Here's how to transform your data into the exact format you need for Salesforce:

Step-by-Step Breakdown

  1. Set Up Pandas: First, ensure pandas is installed and imported.
  2. Replicate Your Input: Let's start by building the sample DataFrame from your provided input.
  3. Melt the Wide Data: Use pd.melt() to pivot all product columns into a single Product column, keeping the account fields as fixed identifiers.
  4. Clean Up Empty Entries: Drop any rows where the Product value is blank (since those accounts don't own that product).
  5. Reset the Index: Adjust the index to match your expected sequential output.

Full Code Implementation

import pandas as pd

# Create the original wide-format DataFrame
data = {
    'Account Name': ['csld solutions', 'global dynamics', 'running inc.'],
    'Acct ID': ['1234', '0256', '2564'],
    'crest': ['', 'crest', ''],
    'ptx': ['ptx', '', ''],
    'gtchange': ['', 'gtchange', 'gtchange'],
    'gtfire': ['gtfire', '', 'gtfire'],
    'dfdata': ['', 'dfdata', '']
}
df = pd.DataFrame(data)

# Reshape to long format using melt()
melted_df = pd.melt(
    df,
    id_vars=['Account Name', 'Acct ID'],  # Columns to retain as identifiers
    value_vars=['crest', 'ptx', 'gtchange', 'gtfire', 'dfdata'],  # Product columns to pivot
    value_name='Product'  # Name for the new consolidated product column
)

# Remove rows with empty product values and reset index
final_df = melted_df[melted_df['Product'] != ''].reset_index(drop=True)

print(final_df)

Resulting Output

Account Name Acct ID    Product
0   csld solutions    1234        ptx
1   csld solutions    1234     gtfire
2  global dynamics    0256      crest
3  global dynamics    0256  gtchange
4  global dynamics    0256     dfdata
5     running inc.    2564  gtchange
6     running inc.    2564     gtfire

This output matches exactly the structure you need—each product gets its own row paired with the full account details, ready for upload to Salesforce.

内容的提问来源于stack exchange,提问作者mtm1186

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:47:48