python - 从两个不同大小的列创建两列

标签 python pandas

我正在尝试从两个列表创建两列。

我尝试使用 zip 构建 DataFrame 并插入列表,但较大的列表被切断。

import pandas as pd

list1 = ['new book revealing', 'library for topic modelling']
list2 = ['potentially embarrassing', 'several international', 'daily newspaper'] 
df = pd.DataFrame(list(zip(list1, list2)), columns =['list1', 'list2']) 

我的输出:

                 list1            list2
0   new book revealing            potentially embarrassing
1   library for topic modelling   several international

良好的输出:

                 list1            list2
0   new book revealing            potentially embarrassing
1   library for topic modelling   several international 
2                                 daily newspaper

最佳答案

一种方法是使用zip_longest :

import itertools
pd.DataFrame(itertools.zip_longest(list1, list2), columns =['list1', 'list2']) 

                        list1                     list2
0           new book revealing  potentially embarrassing
1  library for topic modelling     several international
2                         None           daily newspaper

关于python - 从两个不同大小的列创建两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57769557/

相关文章:

python - Pandas 数据帧 : replace nan values with average of a certain group

python - 不同数据框的模糊匹配列

python - 如何使用 nan 和 twinx/secondary_y 绘制多列线图

python - 用python networkx计算图的入度中心化

python - 捕获来自 Stripe 资金不足的 API 响应

python 将字符串分割成多个分隔符并放入字典中

python - 如何创建一个 tkinter 切换按钮?

python - 减去两个索引不重叠的 DataFrame

python - Pandas:将数据从列添加到另一个数据帧,直到特定时间结束

python - 在 spacy 中保存和加载 nlp 结果