Python Groupby是字符串的一部分

时间:2017-02-01 12:32:28

标签: python pandas

我按照英国邮政编码对交易列表进行分组,但我只想按照邮政编码的第一部分进行分组。因此,英国邮政编码分为两部分,向外和向内,由[空格]分隔。例如W1 5DA。

subtotals = df.groupby('Postcode').count()

我现在正在这样做的方式,我现在想到的方法就是在DataFrame中添加另一列,只输入Postcode列的第一个单词,然后按那......但我想知道是否有更容易的方法。

谢谢

1 个答案:

答案 0 :(得分:4)

我认为您需要groupby Series subtotals = df.groupby(df['Postcode'].str.split().str[0]).count() split创建第一个空格:

df = pd.DataFrame({'Postcode' :['W1 5DA','W1 5DA','W2 5DA']})
print (df)
  Postcode
0   W1 5DA
1   W1 5DA
2   W2 5DA

print (df['Postcode'].str.split().str[0])
0    W1
1    W1
2    W2
Name: Postcode, dtype: object

subtotals = df.groupby(df['Postcode'].str.split().str[0]).count()
print (subtotals)
          Postcode
Postcode          
W1               2
W2               1

样品:

{{1}}

同时检查What is the difference between size and count in pandas?