最佳答案
我想合并几个字符串在一个数据框架基于groupedby在Pandas。
这是我目前为止的代码:
import pandas as pd
from io import StringIO
data = StringIO("""
"name1","hej","2014-11-01"
"name1","du","2014-11-02"
"name1","aj","2014-12-01"
"name1","oj","2014-12-02"
"name2","fin","2014-11-01"
"name2","katt","2014-11-02"
"name2","mycket","2014-12-01"
"name2","lite","2014-12-01"
""")
# load string as stream into dataframe
df = pd.read_csv(data,header=0, names=["name","text","date"],parse_dates=[2])
# add column with month
df["month"] = df["date"].apply(lambda x: x.month)
我希望最终的结果是这样的:
我不明白我如何可以使用groupby,并在“文本”列中应用字符串的某种连接。感谢任何帮助!