我想使用 groupby 对列中的字符串进行某种连接。这是我目前的代码:import pandas as pdfrom io import StringIOdata = StringIO(\'\'\'\'na...
我想使用 groupby 对列中的字符串进行某种连接。
这是我目前的代码:
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)
我希望最终结果如下所示: