如何使用Pandas向NaN字段添加条目?

2024-10-04 09:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.csv文件,其中一列有日语文本,相邻列必须用英语翻译填充。这是我写的剧本。googletranslateapi在我的本地环境(已测试)上运行良好。但是,我无法填充数据文件中的空白单元格。当我在运行脚本之后打开dataframe时,我得到相同的“NaN”值。我附上csv文件以供参考。 https://www.dropbox.com/s/g3x1jvkj2j70ocy/sarthak.csv?dl=0

https://i.stack.imgur.com/zx6Yn.png

import pandas as pd
from google.cloud import translate
client = translate.Client()
df = pd.read_csv('sarthak.csv')
total_rows = df.count()
row_num = df.shape[0]
for i in range(row_num):
    text_to_be_translated = df.iloc[i,7]
    location_for_translated_text = df.iloc[i,6]
    try:
        translated_text_dict = client.translate(text_to_be_translated)
        translated_text = str(translated_text_dict['translatedText'])
        df.set_value(i, 6, translated_text, takeable=False)
    except:
        print("not available")
        pass

Tags: 文件csvtexthttpsimportcomclientdf
1条回答
网友
1楼 · 发布于 2024-10-04 09:24:17

你的表格有错df.set\u值“行。”翻译成“tex”。我相信这是把你扔进你的条件,除了每次迭代。你知道吗

我将研究如何使用apply function,而不是遍历每一行。你知道吗

相关问题 更多 >