简体   繁体   English

Pandas 系列索引作为数据框的列名

[英]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.

相关问题 熊猫使用索引名称作为列名称创建数据框 - Pandas create Dataframe with index name as column name Pandas 数据框按索引选择行,按名称选择列 - Pandas dataframe select row by index and column by name 如何在 Pandas 数据框中为索引添加列名 - How to add column name for index in Pandas dataframe Pandas,如何将系列添加到 DataFrame 列,其中系列索引与 DataFrame 列匹配? - Pandas, how to add Series to DataFrame column, where series index matches a DataFrame column? 从系列或字典向数据帧添加一个新列,将我的系列索引和数据帧列映射到键熊猫 python - Add a new column to a dataframe from a Series or dictionary mapping my series index and a dataframe column to key pandas python 将具有重复索引的系列数据追加到pandas数据框列 - Appending series data with repeated index to pandas dataframe column Pandas:将系列添加到数据框作为列(相同的索引,不同的长度) - Pandas: Add series to dataframe as a column (same index, different length) 将pandas Series作为列添加到多索引的DataFrame填充级别 - Add pandas Series as a column to DataFrame filling levels of multi-index 从多索引熊猫系列创建1列数据框 - Create 1-column dataframe from multi-index pandas series How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame - How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM