简体   繁体   中英

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. The desired df would look as follows:

     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. Any help would be great!

Suppose this series is called 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

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 )

Finally we can create a result dataframe out of the list of dictionaries ( temp_list ) with indices coming from 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM