【问题标题】:Map rows of one dataframe with the column of another dataframe to retrieve the first row of each column from the later table将一个数据框的行与另一个数据框的列映射,以从后面的表中检索每列的第一行
【发布时间】:2018-04-05 11:36:06
【问题描述】:

我有以下数据框:

data = pd.DataFrame({ 'col_A' : [1,2,3,4], 'col_B' : ["x","xx","xxx","y"]});数据

   col_A col_B
0      1     x
1      2    xx
2      3   xxx
3      4     y

我正在尝试创建一个新的数据框,其中包含所有列的概览

  • 每个data.column的数据类型
  • 每一列的第一行
  • 以及每列的一些指标

虽然我设法使用每列的数据类型创建一个新的数据框

DataTypes = pd.DataFrame(data.dtypes, columns= ["Type"]).reset_index().rename(columns={"index": "Column"}); DataTypes

输出:

  Column    Type
0  col_A   int64
1  col_B  object

我很难从数据表和指标中获得第一个值:

期望的输出:

  Column    Type  Value  Max
0  col_A   int64    1     4
1  col_B  object    x    n.a

关于如何将 DataType 表的行与 Data 列映射的任何想法?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    我认为需要concatSeries 创建的dtypesilocmetrics DataFrame 创建的agg

    a = data.dtypes.rename('Type')
    b = data.iloc[0].rename('first')
    c = data.agg(['min','max', 'mean']).T
    
    df = pd.concat([a,b,c], axis=1)
    print (df)
             Type first max mean min
    col_A   int64     1   4  2.5   1
    col_B  object     x   y  NaN   x
    

    【讨论】:

    • data.agg(['min','max', 'mean'])Traceback (most recent call last): File "<ipython-input-134-e4f10b28a814>", line 1, in <module> data.agg(['min','max', 'mean']) File "C:\Users\User\AppData\Local\Continuum\Anaconda3.4\lib\site-packages\pandas\core\generic.py", line 2672, in __getattr__ return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'agg'
    • 这是pandas 0.20.0的新功能,你的pandas版本是多少?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多