为什么我的数据帧会将添加内容复制到特定列?

2024-06-16 09:47:07 发布

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

我正在尝试按顺序向for循环中的数据帧添加新数据。它成功地写入了我(我想)选择的DataFrame列,但它也将数据写入了我不想编辑的另一列。为什么要这么做?我该怎么修

# Resetting the data
supplier_rics = supplier_sheet['RICs']
supplier_rics = supplier_rics.dropna()

# Create a DataFrame object that will contain our headlines
supplier_headlines = pd.DataFrame(data={'RIC':supplier_rics},columns=['RIC','Headline'])
supplier_headlines

for i, ric in supplier_rics.iteritems():
    # Add R: prefix to RICS for news search
    supplier_rics[i] = "R:"+supplier_rics[i]

    # Create the news query
    query = supplier_rics[i] + ' AND (Topic:SIG AND Language:LEN)'

    # Make the Eikon API Call 
    headline = ek.get_news_headlines(query=query,count=1)
    headline_string = headline['text'].to_string(index=False)
    # Add the headline text to the supplier_headlines DataFrame Object
    supplier_headlines.iloc[[i,2]] = headline_string

The original DataFrameThe DataFrame after it has been looped through


Tags: theto数据dataframefordatastringcreate