我有这样的数据集:
pd.DataFrame({'Type.1': ['ES','STR','RRH','ES','STR','STR','STR'],
'Type.2': ['ES','STR','ES','ES','STR','STR','ES'],
'Type.3': ['ES','ES','STR','STR','ES','ES','ES'],
'Type.4': ['ES','ES','STR','STR','ES','ES','ES']})我希望在下面的dataset中添加列,其中包含特定元素的计数(我已经能够像这样在Excel中使用COUNTIF创建该元素)。
+--------+--------+--------+--------+----------+-----------+-----------+
| Type.1 | Type.2 | Type.3 | Type.4 | ES_count | STR_count | RRH_count |
+--------+--------+--------+--------+----------+-----------+-----------+
| ES | ES | ES | ES | 4 | 0 | 0 |
| STR | STR | ES | ES | 2 | 2 | 0 |
| RRH | ES | STR | STR | 1 | 2 | 1 |
| ES | ES | STR | STR | 2 | 2 | 0 |
| STR | STR | ES | ES | 2 | 2 | 0 |
| STR | STR | ES | ES | 2 | 2 | 0 |
| STR | ES | ES | ES | 3 | 1 | 0 |
+--------+--------+--------+--------+----------+-----------+-----------+在Python中最好的方法是什么?我觉得会是这样吗?但不起作用。
for i in range(8):
def function(row):
if row[f"Type.{i-1}"] == 'ES':
row['ES'] = row['ES'] + 1
elif row[f"Type.{i-1}"] == 'RRH':
row['RRH'] = row['RRH'] + 1
elif row[f"Type.{i-1}"] == 'STR':
row['STR'] = row['STR'] + 1
elif row[f"Type.{i-1}"] == 'PSH':
row['PSH'] = row['PSH'] + 1
elif row[f"Type.{i-1}"] == 'TH':
row['TH'] = row['TH'] + 1
df = df.apply(function, axis=1) 谢谢你!!
发布于 2020-06-27 00:46:57
以下是另一个选择:
df_out = pd.get_dummies(df, prefix='', prefix_sep='')
df_out = df_out.groupby(df_out.columns, axis=1).sum().add_suffix('_count')
df.join(df_out)输出:
Type.1 Type.2 Type.3 Type.4 ES_count RRH_count STR_count
0 ES ES ES ES 4 0 0
1 STR STR ES ES 2 0 2
2 RRH ES STR STR 1 1 2
3 ES ES STR STR 2 0 2
4 STR STR ES ES 2 0 2
5 STR STR ES ES 2 0 2
6 STR ES ES ES 3 0 1计时:
%%timeit
df_out = pd.get_dummies(df, prefix='', prefix_sep='')
df_out = df_out.groupby(df_out.columns, axis=1).sum().add_suffix('_count')
df.join(df_out)6.98ms±148 s/环(平均±std )。dev.7次运行中,每一次循环100次)
%%timeit
df2 = df.apply(pd.Series.value_counts, axis=1)
df_out = pd.concat([df,df2],axis=1).fillna(0)
df_out每环9.51ms±403ms(平均±std )。dev.7次运行中,每一次循环100次)
大型数据帧的定时
from timeit import timeit
df = pd.DataFrame({'Type.1': ['ES','STR','RRH','ES','STR','STR','STR'],
'Type.2': ['ES','STR','ES','ES','STR','STR','ES'],
'Type.3': ['ES','ES','STR','STR','ES','ES','ES'],
'Type.4': ['ES','ES','STR','STR','ES','ES','ES']})
def getdummy(d):
df_out = pd.get_dummies(d, prefix='', prefix_sep='')
df_out = df_out.groupby(df_out.columns, axis=1).sum().add_suffix('_count')
return pd.concat([d, df_out], axis=1)
def applyvc(d):
df2 = d.apply(pd.Series.value_counts, axis=1)
return pd.concat([d,df2],axis=1).fillna(0)
res = pd.DataFrame(
index=[10, 30, 100, 300, 1000],
columns='getdummy applyvc'.split(),
dtype=float
)
for i in res.index:
d = pd.concat([df]*i).add_prefix('col')
for j in res.columns:
stmt = '{}(d)'.format(j)
setp = 'from __main__ import d, {}'.format(j)
print(stmt, d.shape)
res.at[i, j] = timeit(stmt, setp, number=100)
# res.groupby(res.columns.str[4:-1], axis=1).plot(loglog=True);
res.plot(loglog=True);

发布于 2020-06-26 03:34:55
下面的代码应该有效。它创建另一个带有事件计数的数据帧,然后将它们连接在一起。
df2 = df.apply(pd.Series.value_counts, axis=1)
df = pd.concat([df,df2],axis=1).fillna(0)https://stackoverflow.com/questions/62585769
复制相似问题