熊猫按其他列的计数填充列

nos*_*mos 2 python dataframe pandas

示例 df:

     company   vehicle registration
0   company1     truck       abc123
1   company1     truck      abcdefg
2   company1       car       234cse
3   company1  forklift          NaN
4   company1     truck        93ds2
5   company2       car      rentall
6   company2       car      rental2
7   company2     truck      rentals
8   company2     truck      rental*
9   company2       car      rental5
10  company3     truck       fdsa23
11  company3     truck        asdf4
12  company3     other       fdsag3
13  company3     other          NaN
14  company3     truck      gls319d
Run Code Online (Sandbox Code Playgroud)

样本数据

我的目标是按公司和车辆类型进行计数(注册和车辆列将被删除)。

我试过这个:

     company   vehicle registration
0   company1     truck       abc123
1   company1     truck      abcdefg
2   company1       car       234cse
3   company1  forklift          NaN
4   company1     truck        93ds2
5   company2       car      rentall
6   company2       car      rental2
7   company2     truck      rentals
8   company2     truck      rental*
9   company2       car      rental5
10  company3     truck       fdsa23
11  company3     truck        asdf4
12  company3     other       fdsag3
13  company3     other          NaN
14  company3     truck      gls319d
Run Code Online (Sandbox Code Playgroud)

从那里我假设某种 groupby 和 sum 函数会合并行和列。

不幸的是,这只会用“1”值填充车辆列,而不是在相应列中填充值。

我想要的输出是:

import pandas as pd

df = pd.read_csv('path to csv', header=0)

df.loc[df.vehicle == 'truck', 'trucks'] = 1
df.loc[df.vehicle == 'car', 'cars'] = 1
df.loc[df.vehicle != 'truck', 'others'] = 1
df.loc[df.vehicle != 'cars', 'others'] = 1
Run Code Online (Sandbox Code Playgroud)

我确定这之前可能已经回答过,但是今天早上我的 google-fu 很弱。

干杯。

jez*_*ael 5

First use Series.map by filtered categories in dictionary and replace all no matched values (NaNs) by Series.fillna.

Then pass to crosstab and if order of output columns is important add DataFrame.reindex:

df['new'] = df.vehicle.map({'truck':'trucks', 'car':'cars'}).fillna('other')
df = pd.crosstab(df['company'], df['new']).reindex(['cars','trucks','other'], axis=1)
print (df)
vehicle   cars  trucks  other
company                      
company1     1       3      1
company2     3       2      0
company3     0       3      2
Run Code Online (Sandbox Code Playgroud)