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

问询:将Pandas DataFrame两类数据统计生成透视表的方法

Hey there! Let's tackle this pivot table task step by step. From your sample data, it looks like you want to count occurrences of each FreqWord term across the Classified categories (Positive/Negative) and turn that into a clean, readable pivot table. Here's a tailored solution that fits your exact needs:

Step 1: Prepare your DataFrame for analysis

First, we need to handle cases where FreqWord has multiple words (like "calm love")—we'll split those into individual rows so each word can be counted separately. We'll also clean up any empty values to avoid skewing our counts:

import pandas as pd

# Recreate your sample DataFrame (replace this with your actual data)
data = {
    'Tweets': [
        "calm director day science meetings nasal talk cutting edge remote sensing research drought veg fluorescence",
        "drought love thought drought",
        "drought reign mother kerr funny none tried make come back drought",
        "drought wonder could help thai market b post reuters drought devastates south europe crops"
    ],
    'Classified': ['Positive', 'Positive', 'Positive', 'Negative'],
    'FreqWord': ['calm love', '', '', '']
}
df = pd.DataFrame(data)

# Split multi-word FreqWord entries into individual rows, then remove empty values
df_clean = df.assign(FreqWord=df['FreqWord'].str.split())\
             .explode('FreqWord')\
             .replace('', pd.NA)\
             .dropna(subset=['FreqWord'])
Step 2: Generate the pivot table

Now we'll use Pandas' pivot_table() to aggregate the counts by FreqWord and Classified. We'll fill any missing counts with 0 for clarity, and optionally add a total column to see overall word frequency:

# Build the pivot table
pivot_table = pd.pivot_table(
    df_clean,
    index='FreqWord',  # Rows will be individual words from FreqWord
    columns='Classified',  # Columns will be your Positive/Negative categories
    values='Tweets',  # We use this column just to count occurrences (any column works)
    aggfunc='count',  # Aggregation method: count how many times each word appears per category
    fill_value=0  # Replace missing values with 0 instead of NaN
)

# Optional: Add a Total column to see overall word counts
pivot_table['Total'] = pivot_table.sum(axis=1)
Step 3: View the result

For your sample data, the output will look like this:

FreqWordPositiveNegativeTotal
calm101
love101

If your actual dataset has more FreqWord entries across both Positive and Negative categories, this solution will scale seamlessly.

A quick tip: If you need to adjust how missing values are handled (e.g., keep empty FreqWord entries instead of dropping them), you can remove the .replace('', pd.NA).dropna(subset=['FreqWord']) lines and adjust the fill_value in the pivot table as needed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:25:04