python - Pandas:将数据框除以列中的某些值

标签 python pandas scikit-learn

我有数据框

date    city    brand   model   count
2016-02 abakan  audi    a6  1
2016-02 abakan  bmw 5-series    2
2016-02 abakan  bmw x5  2
2016-02 abakan  chery   a15 1
2016-02 abakan  chevrolet   cruze   3
2016-02 abakan  chevrolet   cruze   10

我需要将其划分为更多的小数据帧,以便使用 sklearn 中的线性回归。有什么方法可以做到这一点,或者有某种方法可以指定它线性回归来考虑列中的不同值?

date    city    brand   model   count
2016-02 abakan  audi    a6  1

date    city    brand   model   count
2016-02 abakan  bmw 5-series    2

date    city    brand   model   count
2016-02 abakan  bmw x5  2

date    city    brand   model   count
2016-02 abakan  chery   a15 1

date    city    brand   model   count
2016-02 abakan  chevrolet   cruze   3
2016-02 abakan  chevrolet   cruze   10

我怎样才能做到这一点?

最佳答案

Pandas 解决方案是 groupby列表理解 - 输出是DataFrames列表:

dfs = [g for i, g in df.groupby(['date','city','brand','model'])]
print (dfs)
[      date    city brand model  count
0  2016-02  abakan  audi    a6      1,       date    city brand     model  count
1  2016-02  abakan   bmw  5-series      2,       date    city brand model  count
2  2016-02  abakan   bmw    x5      2,       date    city  brand model  count
3  2016-02  abakan  chery   a15      1,       date    city      brand  model  count
4  2016-02  abakan  chevrolet  cruze      3
5  2016-02  abakan  chevrolet  cruze     10]

print (dfs[0])
      date    city brand model  count
0  2016-02  abakan  audi    a6      1

关于python - Pandas:将数据框除以列中的某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137570/

相关文章:

python - 尝试安装 python3 但终端始终显示为 python 2.7.13

indexing - Pandas 数据框 : how to match on multiple index levels when doing arithmetic operations involving two dataframes

python - 使用 matplotlib + pandas 进行多元二因子设计的分组箱线图

python-3.x - 适用于 Python 的逻辑回归和 KNN 等模型的输入格式

python - 将数据框与其中一列中的字典数据连接起来

python - 如何等待协程直到满足条件?

python - 我可以使用进程间通信在 64 位 python 中调用这个 32 位 dll 吗?

python - 如何在不覆盖数据的情况下写入现有的 excel 文件(使用 pandas)?

python - 如何计算 Timeseries DataFrame 的概率?

python - 如何为机器学习预处理数据?