pandas “转置”一个熊猫系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43517338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"transpose" a Pandas Series
提问by lazy1
I have a DataFrame with an ID column and some features columns. I'd like to see a description of how many unique IDs are there per column values.
我有一个带有 ID 列和一些功能列的 DataFrame。我想查看每列值有多少个唯一 ID 的描述。
The following code works but I wonder if there a better way than the to_frame().unstack().unstack()
line which transposes the .describe()
series result to DataFrame where the columns are the percentiles, max, min ...
以下代码有效,但我想知道是否有比to_frame().unstack().unstack()
将.describe()
系列结果转置为 DataFrame的行更好的方法,其中列是百分位数、最大值、最小值......
def unique_ids(df):
rows = []
for col in sorted(c for c in df.columns if c != id_col):
v = df.groupby(col)[id_col].nunique().describe()
v = v.to_frame().unstack().unstack() # Transpose
v.index = [col]
rows.append(v)
return pd.concat(rows)
回答by jezrael
It seems you need change:
看来你需要改变:
v = v.to_frame().unstack().unstack()
to
到
v = v.to_frame().T
Or is possible transpose
final DataFrame
, also is added rename
by col
:
或可能transpose
最终DataFrame
也加入rename
的col
:
df = pd.DataFrame({'ID':[1,1,3],
'E':[4,5,5],
'C':[7,8,9]})
print (df)
C E ID
0 7 4 1
1 8 5 1
2 9 5 3
def unique_ids(df):
rows = []
id_col = 'ID'
for col in sorted(c for c in df.columns if c != id_col):
v = df.groupby(col)[id_col].nunique().describe().rename(col)
rows.append(v)
return pd.concat(rows, axis=1).T
print (unique_ids(df))
count mean std min 25% 50% 75% max
C 3.0 1.0 0.000000 1.0 1.00 1.0 1.00 1.0
E 2.0 1.5 0.707107 1.0 1.25 1.5 1.75 2.0