c=pd.DataFrame(c) d=c[1].value_counts() print(d) d=pd.DataFrame(d) print(d[~d.index.str.contains("normal")]) e=d[~d.index.str.contains("normal")] f=d[d.index.str.contains("normal")]怎么把f中含有e中除normal外行名一样的数据整合在一起
时间: 2023-06-15 07:05:50 浏览: 50
你可以使用 Pandas 的 merge() 函数将两个 DataFrame 合并,以行名作为连接的键。具体代码如下:
```
merged = pd.merge(e, f[f.index.isin(e.index)], left_index=True, right_index=True)
```
这个代码将 e 和 f 中行名相同的部分合并到了一起,并返回一个新的 DataFrame merged。其中,left_index 和 right_index 参数分别指定连接时使用左侧和右侧 DataFrame 的行名作为键。isin() 函数用于判断 f 中的行名是否在 e 中出现过。
相关问题
#统计概率各自出现的次数 df=pd.DataFrame(data_info01) counts=df['PRED_PROB'].value_counts() counts_df=counts.to_frame().reset_index().rename(columns={'index':'概率','数量':'count'}) print(counts_df.head(10))优化代码:使之成为统计100%-90%概率,90%-80%概率等之间的数量
要统计不同概率值的出现次数,可以使用`value_counts()`函数。以下是优化后的代码,用于统计不同概率区间的数量:
```python
import pandas as pd
# 创建一个示例的DataFrame
df = pd.DataFrame(data_info01)
# 统计概率各自出现的次数
counts = df['PRED_PROB'].value_counts().sort_index(ascending=False)
# 创建概率区间
prob_intervals = [(x / 10, (x - 10) / 10) for x in range(100, 80, -10)]
# 统计每个概率区间的数量
counts_df = pd.DataFrame(columns=['概率区间', '数量'])
for interval in prob_intervals:
count = counts.loc[(df['PRED_PROB'] <= interval[0]) & (df['PRED_PROB'] > interval[1])].sum()
counts_df = counts_df.append({'概率区间': f'{interval[0]*10}% - {interval[1]*10}%', '数量': count}, ignore_index=True)
print(counts_df)
```
这段代码会按照每个概率区间统计数量,例如统计100%-90%概率、90%-80%概率等之间的数量。输出结果会给出每个概率区间的数量。
#统计概率各自出现的次数 df=pd.DataFrame(data_info01) counts=df['PRED_PROB'].value_counts() counts_df=counts.to_frame().reset_index().rename(columns={'index':'概率','数量':'count'}) print(counts_df.head(10))优化代码:使之成为统计1-0.9概率,0.9-0.8概率等之间的数量
要优化代码以统计不同概率区间的数量,可以使用`pd.cut()`函数来将概率分箱,并使用`groupby()`函数进行分组和计数。以下是优化后的代码示例:
```python
import pandas as pd
# 创建一个示例的DataFrame
df = pd.DataFrame(data_info01)
# 将概率值分箱成不同区间
bins = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0]
labels = ['1-0.9', '0.9-0.8', '0.8-0.7', '0.7-0.6', '0.6-0.5',
'0.5-0.4', '0.4-0.3', '0.3-0.2', '0.2-0.1', '0.1-0']
df['概率区间'] = pd.cut(df['PRED_PROB'], bins=bins, labels=labels, right=False)
# 统计每个概率区间的数量
counts = df['概率区间'].value_counts().sort_index().reset_index()
counts.columns = ['概率区间', '数量']
print(counts.head(10))
```
这段代码会将概率值分为10个区间,并统计每个区间的数量。输出结果会给出每个概率区间及其对应的数量。注意,这里使用了`pd.cut()`函数将概率值分箱,然后使用`value_counts()`函数统计每个区间的数量,并最后使用`sort_index()`函数对结果按照概率区间排序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)