python - Pandas 将列表列表转换为列名并附加值

标签 python pandas dataframe data-cleaning

我必须在 Pandas 数据框中的列中,一个键第二个带有值,其中两个都是列表列表。
像这样:

import pandas as pd 
example = pd.DataFrame( {'col1': [['key1','key2','key3'],['key1','key4'],['key1', 'key3', 'key4','key5']], 'col2': [['value1','value2','value3'], ['value1','value4'], ['value1', 'value3', 'value4','value5']]  }) 
print(example)
    col1    col2
0   [key1, key2, key3]  [value1, value2, value3]
1   [key1, key4]    [value1, value4]
2   [key1, key3, key4, key5]    [value1, value3, value4, value5]
首先,我想将所有可能的键转换为列,将值附加到它们。
最终结果应该是这样的
    key1      key2    key3     key4    key5
0   value1    value2  value3   NaN     NaN
1   value1    NaN     NaN      value4  NaN
2   value1    NaN     value3   value4  value5
        

最佳答案

尝试使用 explode 并 reshape 数据框。

#pandas 1.3.0 update
df_new = example.explode(['col1', 'col2'])
#df_new = example.apply(pd.Series.explode)    
df_new.set_index('col1', append=True).unstack()
输出:
col1    key1    key2    key3    key4    key5
0     value1  value2  value3     NaN     NaN
1     value1     NaN     NaN  value4     NaN
2     value1     NaN  value3  value4  value5

关于python - Pandas 将列表列表转换为列名并附加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289454/

相关文章:

python - 合并不同列上的两个数据框

python - Plotly 似乎丢失了热图的标签

python - 数据框列标准错误IndexEngine

python - 无法绘制饼图

python - 如何执行这样的pandas聚合: drop nan and concatenate to the first?

Pandas:根据另一列中的值对两列进行分组

根据多个条件替换 data.frame 上的值

python - 迭代 Pandas DataFrame 与迭代其列名相同吗?

python - pip install --upgrade 与 pip uninstall 然后安装 : is there ever any difference?

Python 正则表达式 : find a substring that doesn't contain a substring