为Pandas DataFrame添加超链接后对象变为NoneType而非DataFrame
Hey there! Let's break down why you're hitting this AttributeError: 'NoneType' object has no attribute 'columns' and walk through fixes to get your JoogleChart dropdown working with your hyperlink-enabled DataFrame.
What's Causing This?
This error means somewhere in your code, the object you're trying to access .columns on has become None. In your case, it's almost certainly the DataFrame you're passing to JoogleChart—either you accidentally overwrote it to None, or you're passing a non-DataFrame object (like a Styler) that JoogleChart can't process properly.
Step-by-Step Fixes
1. Stop Confusing DataFrames with Styler Objects
When you add hyperlinks using Pandas' style API (e.g., df.style.format(...)), you get a Styler object back—not the original DataFrame. JoogleChart expects a raw Pandas DataFrame, not a Styler. If you tried passing that Styler to JoogleChart, internal processing will fail and return None, leading to the .columns error.
Do this instead to keep your hyperlinks in a DataFrame:
# Add hyperlinks as string values in a DataFrame column (preserves the DataFrame type) import pandas as pd df = pd.DataFrame({ "Item": ["Laptop", "Phone"], "URL": ["https://example.com/laptop", "https://example.com/phone"] }) # Create a clickable link column (returns a DataFrame, not a Styler) df["Clickable_Link"] = df.apply( lambda row: f'<a href="{row["URL"]}">{row["Item"]}</a>', axis=1 )
2. Fix Accidental None Assignments
Pandas methods with inplace=True return None—if you assign that result back to your DataFrame variable, you'll overwrite it to None. For example:
# ❌ This sets df to None! df = df.drop(columns=["Unwanted_Col"], inplace=True) # ✅ Do this instead (no assignment needed for inplace=True) df.drop(columns=["Unwanted_Col"], inplace=True) # OR ✅ Assign the returned modified DataFrame (no inplace) df = df.drop(columns=["Unwanted_Col"])
Double-check all your DataFrame operations to make sure you aren't accidentally turning your df into None.
3. Validate Your DataFrame Before Passing to JoogleChart
Add quick checks to confirm your DataFrame is valid right before you use it with JoogleChart:
# Verify df isn't None and has columns assert df is not None, "Oops! Your DataFrame became None somewhere." print(df.columns) # This should print your column names without errors
If this fails, trace back through your code to find where the DataFrame got corrupted.
4. Example Working Code
Putting it all together, here's how your flow should look:
import pandas as pd from jooglechart import JoogleChart # 1. Build DataFrame with hyperlinks (as string columns) df = pd.DataFrame({ "Product": ["Laptop", "Phone", "Tablet"], "Purchase_Link": [ "https://example.com/laptop", "https://example.com/phone", "https://example.com/tablet" ] }) df["Clickable_Link"] = df.apply( lambda row: f'<a href="{row["Purchase_Link"]}">{row["Product"]}</a>', axis=1 ) # 2. Validate DataFrame (optional but helpful) assert df is not None, "DataFrame is missing!" # 3. Create JoogleChart with dropdown chart = JoogleChart(df, chart_type="Table") # Add dropdown using the valid DataFrame's columns chart.add_dropdown("Select Column", df.columns, default_value="Clickable_Link") chart.draw()
Final Check
Always make sure the object you're passing to JoogleChart's dropdown methods is a raw, non-None Pandas DataFrame. The most common pitfalls are mixing up Styler objects with DataFrames, or accidentally overwriting your df with inplace=True results.
内容的提问来源于stack exchange,提问作者Marisa Sievers

