簡體   English   中英

按pandas系列中的數據類型對列進行分組會引發TypeError:數據類型未被理解

[英]Grouping columns by data type in pandas series throws TypeError: data type not understood

我按類型對值進行分組如下:

groups = frame.columns.to_series().groupby(frame.dtypes).groups

我得到錯誤:

TypeError: data type not understood

通過數據類型對列進行分組以防止此類錯誤的正確方法是什么?

編輯:示例輸入

0             0       0  1985     ATL        NL  barkele01  870000         428.0   428.0      1955.0    ...           Leonard Harold   225.0   77.0    R      R  1976-09-14  1987-09-26  barkl001  barkele01       both
1             1       1  1985     ATL        NL  bedrost01  550000         559.0   559.0      1957.0    ...            Stephen Wayne   200.0   75.0    R      R  1981-08-14  1995-08-09  bedrs001  bedrost01       both
2             2       2  1985     ATL        NL  benedbr01  545000         614.0   614.0      1955.0    ...              Bruce Edwin   175.0   73.0    R      R  1978-08-18  1989-09-11  beneb001  benedbr01       both
3             3       3  1985     ATL        NL   campri01  633333           NaN     NaN         NaN    ...                      NaN     NaN    NaN  NaN    NaN         NaN         NaN       NaN        NaN  left_only
4             4       4  1985     ATL        NL  ceronri01  625000        1466.0  1466.0      1954.0    ...             Richard Aldo   192.0   71.0    R      R  1975-08-17  1992-07-10  ceror001  ceronri01       both
5             5       5  1985     ATL        NL  chambch01  800000        1481.0  1481.0      1948.0    ...      Carroll Christopher   195.0   73.0    L      R  1971-05-28  1988-05-08  chamc001  chambch01       both

樣本輸出就像是一樣

{float: [columns], int:[columns], string:[columns]}

您可以沿axis=1使用groupby

type_dct = {str(k): list(v) for k, v in df.groupby(df.dtypes, axis=1)}

對於您的示例數據框,這給出了:

{'int64': [0, 1, 2, 3, 7],
 'float64': [8, 9, 10, 14, 15],
 'object': [4, 5, 6, 11, 12, 13, 16, 17, 18, 19, 20, 21, 22]}

請注意,沒有像Pandas那樣的string系列dtype object dtype表示指向任意Python對象的指針,包括字符串。 請參閱DataFrame中的字符串,但dtype是對象以獲取更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM