我有一列索引,每個索引都有它對應的詞:
我有一列索引,每個索引都有它對應的詞。
| id | 詞 |
|---|---|
| 1 | word1 |
以此類推。
我想以這種方式按索引分組。 for id 1-[word1,word2,word3]。 為id 2-[word4,word5]
以此類推
并提取到CSV檔案中
我有這樣的代碼:
df = pd.DataFrame(data)
d={"word":"first"}。
df_new = df.groupby(df['id'], as_index=False).aggregate(d).reindex(columns=df['word'] )
print (df_new)
df_new.to_csv('test.csv', sep=' ', encoding='utf-8', index=False)
我需要改變什么才能讓它發揮作用?
謝謝你的幫助。
提前感謝你
uj5u.com熱心網友回復:
# Import Dependencies。
import pandas as pd
# 創建資料框架 # 創建資料框架
data = {'id': [1, 1, 1, 2, 2], 'word'/span>: ['word1'/span>, 'word2'/span>, 'word3'/span>, 'word4'/span>, 'word5'/span>]}。
df = pd.DataFrame(data)
# Groupby and Merge'id', as_index=False).agg({'word' : ', ' .join})
# Result。
id word
0 1 字1、字2、字3
1 2 word4,word5
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/325363.html
標籤:
上一篇:單擊按鈕超過3次后阻止用戶
