合并Pandas中的数据

问题描述 投票:0回答:1

我正在研究一个大型数据集,其中包括消息和回复消息。

我需要将消息列和回复消息合并到第三列中。但我无法按照我的意愿去做。

我的预期输出如下:-

S.否 留言 评论
1 你好 你好吗?
2 你好 我很好,你好吗?
3 你好 我很好
4 你好 你也很好。
S.否 新_专栏
1 你好
2 你好吗?
3 我很好,你好吗?
4 我很好
5 你也很好。

我用了两套代码

第一个代码

new_column = []
current_message = None

for index, row in df.iterrows():
    if current_message is None:
        new_column.append(row['Message'])
        new_column.append(row['Comment'])
        current_message = row['Message']
    else:
        new_column.append(row['Comment'])
        current_message = row['Message']

# Add the first comment as an additional row at the end
new_column.append(df.iloc[0]['Comment'])

# Create a new DataFrame with the new column
new_column_df = pd.DataFrame(new_column, columns=['New_Column'])

# Save the DataFrame to a new CSV file
new_column_df.to_csv('New_Column.csv', index=False)

第二个代码

data['comment_text'] = data['comment_text'].fillna('')


# 2. Now, we can apply the operation of merging comment with message text.
# Create a new column by combining 'message' and 'comment_text' only for rows with comments
data['combined'] = data['message'].str.cat(data['comment_text'], sep='\n', na_rep='')

# Display the resulting DataFrame
print(data[['combined']])

这两个代码都无助于实现我想要实现的目标,如图所示。

pandas append
1个回答
0
投票
new_column_df = pd.DataFrame(
    {
        'S.No': np.arange(1, len(df) + 2),
        'New_Column': np.insert(df['Comment'],
                                0, values=df.loc[0, 'Message'])
    }
)

print(new_column_df)

输出:

   S.No              New_Column
0     1                   hello
1     2            How are you?
2     3  I am good How are you?
3     4               I am fine
4     5      You are also fine.
© www.soinside.com 2019 - 2024. All rights reserved.