You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Google Colab中使用Pandas及googletrans翻译CSV/Excel文件时,如何保留原格式并正常导出CSV

Fixing DataFrame Format After Translating with googletrans in Pandas

The issue you're facing comes from how you're applying the translation function to your entire DataFrame. When you use df.apply(translator.translate, src='en', dest='es'), you're applying the translator to each column (not each cell), which returns a structure of Translated objects that doesn't align with your original table layout. The subsequent apply(getattr, args=('text',)) can't fix this structural mismatch, leading to the messy CSV-like output.

Here's a revised approach that preserves your original DataFrame structure perfectly:

import pandas as pd
from googletrans import Translator

# Read the Excel file as before
df = pd.read_excel('/content/Libro2.xlsx')
translator = Translator()

# Define a helper function to handle translation and null values
def translate_single_cell(text):
    # Skip empty cells to avoid errors
    if pd.isna(text):
        return text
    # Translate and extract the text result
    translated = translator.translate(text, src='en', dest='es')
    return translated.text

# Use applymap to process every cell individually, keeping the original structure
df_translated = df.applymap(translate_single_cell)

# Now you can export the translated DataFrame normally, matching your input format
df_translated.to_csv('/content/translated_output.csv', index=False)

Key improvements here:

  • applymap instead of apply: This method operates on every single cell in your DataFrame, ensuring the row/column layout, column names, and data structure stay identical to your input Excel file.
  • Null value handling: The helper function checks for empty cells (NaN) and returns them as-is, preventing translation errors that would break your code.
  • Clean export: Using index=False in to_csv ensures you don't write the DataFrame's index column to the output CSV, keeping it aligned with your original input format.

With this code, your translated DataFrame will behave exactly like a regular Pandas DataFrame—you can manipulate it further or export it to CSV/Excel without any format issues.

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

火山引擎 最新活动