[英]Pandas - transpose lists with unequal length in the value of dataframe
这个问题是对Pandas的扩展:将列中的列表分成多行 ,现在我不想合并更多的DataFrame。 而且我无法使其与超过2个dfs一起使用。
我有这个DataFrame:
Index Job positions Job types Locations
0 [5] [6] [3, 4, 5]
1 [1] [2, 6] [3, NaN]
2 [1,3] [9, 43] [1]
我想要数字的每个单一组合,因此最终结果将是:
index Job position Job type Location
0 5 6 3
0 5 6 4
0 5 6 5
1 1 2 3
1 1 2 NaN
1 1 6 3
1 1 6 NaN
2 1 9 1
2 1 43 1
2 3 9 1
2 3 43 1
所以我要做的是将列转换为Series:
positions = df['Job positions'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
types = df['Job types'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
locations = df['Locations'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
dfs = [positions, types, locations]
然后尝试像这样合并它们:
df_final = reduce(lambda left,right: pd.merge(left,right,left_index=True, right_index=True, how="left"), dfs)
但似乎是用NaN跳过了这些字段-如何防止这种情况?
1行:
import itertools
dfres = pd.DataFrame([(i[0],)+j for i in df.values for j in itertools.product(*i[1:])]
,columns=df.columns).set_index('index')
Job positions Job types Locations
index
0 5 6 3
0 5 6 4
0 5 6 5
1 1 2 3
1 1 2 NaN
1 1 6 3
1 1 6 NaN
2 1 9 1
2 1 43 1
2 3 9 1
2 3 43 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.