[英]Pandas series index as column name of dataframe
Task description任务描述
I have a pandas series as follows:我有一个熊猫系列如下:
rank loc
0.0 AU 2
US 1
1.0 UK 1
AU 3
US 1
I wish to make a DataFrame with rank as the column name and loc as the index.我希望制作一个以 rank 作为列名和 loc 作为索引的 DataFrame。 The desired df would look as follows:
所需的 df 如下所示:
0.0 1.0
AU 2 3
UK 1
US 1 1
I am happy with either NaN or 0 in rows where there are no values.我对没有值的行中的 NaN 或 0 感到满意。 Any help would be great!
任何帮助都会很棒!
Suppose this series is called S.假设这个系列被称为 S。
First flatten it and convert to a dataframe and rename for easier access首先将其展平并转换为数据框并重命名以便于访问
df = pd.DataFrame(pd.DataFrame(S).to_records())
df.columns = ['rank', 'loc', 'counts']
Now group by loc , and loop over each group and create a dictionary, with "key" coming from rank and "value" coming from counts现在按loc分组,并遍历每个组并创建一个字典,其中“键”来自排名,“值”来自计数
For each group you will have this dictionary, which you can append to a temp_list , while temp_indices keep track of the index (in this case the value of loc )对于每个组,您将拥有这个字典,您可以将其附加到temp_list ,而temp_indices跟踪索引(在这种情况下是loc的值)
Finally we can create a result dataframe out of the list of dictionaries ( temp_list ) with indices coming from temp_indices最后,我们可以从字典列表( temp_list )中创建一个结果数据帧,索引来自temp_indices
temp_list = list()
temp_indices = list()
for _name, _val in df.groupby('loc'):
temp_dict = dict()
for _, row in _val.iterrows():
temp_dict.update({row['rank']: row['counts']})
temp_indices.append(_name)
temp_list.append(temp_dict)
result = pd.DataFrame(temp_list, index=temp_indices)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.