问询:将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:
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'])
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)
For your sample data, the output will look like this:
| FreqWord | Positive | Negative | Total |
|---|---|---|---|
| calm | 1 | 0 | 1 |
| love | 1 | 0 | 1 |
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

