python - Pandas/Python - 如何按对象查看组的所有行或记录?

标签 python python-3.x pandas group-by

我有这样的代码,

x.groupby(x['interval_start_time'])['acceptance_rate'].apply(lambda x: np.percentile(x, [5,10,25,50,75,90,95]).round(2))

其结果输出看起来像,

enter image description here

我如何查看所有这些?而不是中间有 .. ?谢谢

最佳答案

尝试设置以下 2 个选项。

  • pd.set_option("display.max_columns", df.columns.shape[0])

  • pd.set_option("display.max_rows", df.index.shape[0])

我在下面提供了一个简单的示例,它可能会帮助您解决这个问题。只要看一下、​​理解并尝试即可。

>>> import pandas as pd
>>> 
>>> d = {
...     "fullname": ["R A", "X Y", "P K", "X Y", "D R", "R A", "R E", "X Y"],
...     "age": [26, 25, 50, 34, 67, 89, 50, 26],
...     "profession": ["Python Developer", "Node Developer", "Django Developer", "JS Developer", "Python Developer", "Node Developer", "Node Developer", "Python Developer"]
... }
>>> 
>>> df = pd.DataFrame(d)
>>> df
  fullname  age        profession
0      R A   26  Python Developer
1      X Y   25    Node Developer
2      P K   50  Django Developer
3      X Y   34      JS Developer
4      D R   67  Python Developer
5      R A   89    Node Developer
6      R E   50    Node Developer
7      X Y   26  Python Developer
>>> 
>>> group1 = df.groupby("fullname")
>>> group2 = df.groupby("age")
>>> group3 = df.groupby("profession")
>>> 
>>> group1
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x101209a90>
>>> 
>>> group1.groups
{'D R': Int64Index([4], dtype='int64'), 'P K': Int64Index([2], dtype='int64'), 'R A': Int64Index([0, 5], dtype='int64'), 'R E': Int64Index([6], dtype='int64'), 'X Y': Int64Index([1, 3, 7], dtype='int64')}
>>> 
>>> group1.get_group("R A")
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
>>> 
>>> group1.get_group("R E")
  fullname  age      profession
6      R E   50  Node Developer
>>> 
>>> group1.get_group("P K")
  fullname  age        profession
2      P K   50  Django Developer
>>> 
>>> for group in group1.groups:
...     print(group)
... 
D R
P K
R A
R E
X Y
>>> for group in group1.groups:
...     print(group1.get_group(group))
... 
  fullname  age        profession
4      D R   67  Python Developer
  fullname  age        profession
2      P K   50  Django Developer
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
  fullname  age      profession
6      R E   50  Node Developer
  fullname  age        profession
1      X Y   25    Node Developer
3      X Y   34      JS Developer
7      X Y   26  Python Developer
>>> 
>>> group1.get_group("R A")
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
>>> 
>>> pd.set_option("display.max_columns", 2)
>>> group1.get_group("R A")
  fullname        ...               profession
0      R A        ...         Python Developer
5      R A        ...           Node Developer

[2 rows x 3 columns]
>>> 
>>> ra_group = group1.get_group("R A")
>>> ra_columns = ra_group.columns
>>> ra_columns
Index(['fullname', 'age', 'profession'], dtype='object')
>>> 
>>> ra_columns.ndim
1
>>> ra_columns.size
3
>>> ra_columns.shape
(3,)
>>> 
>>> pd.set_option("display.max_columns", ra_columns.shape[0])
>>> group1.get_group("R A")
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
>>> 
>>> ra_group
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
>>> 
>>> ra_group.index
Int64Index([0, 5], dtype='int64')
>>> 
>>> ra_group.index.shape
(2,)
>>> pd.set_option("display.max_rows", ra_group.index.shape[0])
>>> ra_group
  fullname  age        profession
0      R A   26  Python Developer
5      R A   89    Node Developer
>>>
>>> pd.set_option("display.max_rows", ra_group.index.shape[0] - 1)
>>> ra_group
   fullname  age        profession
0       R A   26  Python Developer
..      ...  ...               ...

[2 rows x 3 columns]
>>> 

关于python - Pandas/Python - 如何按对象查看组的所有行或记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945480/

相关文章:

python - 在 PuLP(Python) 中指定 lowBound 和 upBound

python - pytest -> 如何在类下的测试方法中使用 fixture 返回值

python-3.x - 识别 git 提交的实际分支名称

Python将多列 Pandas 数据框转换为单值表数据框

python - Pandas 和 Plotly : how to access data columns in the hover text that are not used to plot the point?

python - 删除差异但保持 python 列表中的顺序和重复项

python - 从给定的系列中获取 python 中的随机模块种子?

python - 如何在 python 中使用 keras 获得概率/置信度作为 CNN 的输出?

python-3.x - docker 。无法建立新连接 : [Errno 111] Connection refused'

python - 从嵌套字典获取 pandas 数据框?