您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 在Pandas中复杂(对我而言)从宽到长重塑

5b51 2022/1/14 8:22:35 python 字数 4467 阅读 517 来源 www.jb51.cc/python

个人(索引从0到5)在两个位置之间进行选择:A和B. 我的数据具有宽格式,其中包含因个体(ind_var)而异的特征以及仅因位置(location_var)而异的特征. 例如,我有: In [281]: df_reshape_test = pd.DataFrame( {'location' : ['A', 'A', 'A', 'B', 'B', 'B'], 'dist_to_A' : [0, 0

概述

例如,我有

In [281]:

df_reshape_test = pd.DataFrame( {'location' : ['A','A','B','B'],'dist_to_A' : [0,50,50],'dist_to_B' : [50,0],'location_var': [10,10,14,14],'ind_var': [3,8,1,3,4]})

df_reshape_test

Out[281]:
    dist_to_A   dist_to_B   ind_var location location_var
0    0            50             3   A       10
1    0            50             8   A       10
2    0            50            10   A       10
3    50           0              1   B       14
4    50           0              3   B       14
5    50           0              4   B       14

变量“location”是个人选择的变量.
dist_to_A是距离个人选择的位置到位置A的距离(与dist_to_B相同)

我希望我的数据有这样的形式:

choice  dist_S  ind_var location    location_var
0    1        0       3         A           10
0    0       50       3         B           14
1    1        0       8         A           10
1    0       50       8         B           14
2    1        0      10         A           10
2    0       50      10         B           14
3    0       50       1         A           10
3    1        0       1         B           14
4    0       50       3         A           10
4    1        0       3         B           14
5    0       50       4         A           10
5    1        0       4         B           14

其中choice == 1表示个人已选择该位置,dist_S表示距所选位置的距离.

我读到了.stack方法,但无法弄清楚如何将其应用于此案例.
谢谢你的时间!

注意:这只是一个简单的例子.我正在寻找的数据集每个位置都有不同数量的位置和个体数量,所以我正在寻找一个灵活的解决方案,如果可能的话

df = pd.DataFrame( {'location' : ['A',4]})

df['ind'] = df.index

#The `location` and `location_var` corresponds to the choices,#record them as dictionaries and drop them 
#(Just realized you had a cleaner way,copied from yous). 

ind_to_loc = dict(df['location'])
loc_dict = dict(df.groupby('location').agg(lambda x : int(np.mean(x)))['location_var'])
df.drop(['location_var','location'],axis = 1,inplace = True)
# Now reshape
df_long = pd.wide_to_long(df,['dist_to_'],i = 'ind',j = 'location') 

# use the dictionaries to get variables `choice` and `location_var` back.

df_long['choice'] = df_long.index.map(lambda x: ind_to_loc[x[0]])
df_long['location_var'] = df_long.index.map(lambda x : loc_dict[x[1]])
print df_long.sort()

这为您提供了您要求的表格:

ind_var  dist_to_ choice  location_var
ind location                                        
0   A               3         0      A            10
    B               3        50      A            14
1   A               8         0      A            10
    B               8        50      A            14
2   A              10         0      A            10
    B              10        50      A            14
3   A               1        50      B            10
    B               1         0      B            14
4   A               3        50      B            10
    B               3         0      B            14
5   A               4        50      B            10
    B               4         0      B            14

当然,如果这是你想要的,你可以生成一个取0和1的选择变量.

总结

以上是编程之家为你收集整理的python – 在Pandas中复杂(对我而言)从宽到长重塑全部内容,希望文章能够帮你解决python – 在Pandas中复杂(对我而言)从宽到长重塑所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶