pandas stack

import pandas as ps 
df = pd.DataFrame([[0, 1], [2, 3]],
                                    index=['cat', 'dog'],
                                    columns=['weight', 'height'])
     weight  height
cat       0       1
dog       2       3
def price_dict():
    import pandas as pd
    roph=path_dir()
    df=pd.read_table(roph+'/train_data/exchange_price.csv')
    df1=df.values

    df2=pd.DataFrame(df1[1:],columns=df1[0])

    price_index=[int(p) for p in df2.id]
    price_value=[float(p) for p in df2.us_price]

    price_map=dict(zip(price_index,price_value))
    return price_map


df1=df.stack().reset_index()

df1

  level_0 level_1  0
0     cat  weight  0
1     cat  height  1
2     dog  weight  2
3     dog  height  3
posted @ 2022-08-19 22:50  luoganttcc  阅读(5)  评论(0)    收藏  举报