在Pandas中创建 Dataframe ,为什么创建会导致元组的压缩被清除?[重复]

rhfm7lfc  于 2023-01-11  发布在  其他
关注(0)|答案(1)|浏览(108)
    • 此问题在此处已有答案**:

Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?(5个答案)
1年前关闭.

import pandas as pd
sentences=['aaa','bbb','ccc']
labels = [1,2,3]
infos = zip(sentences , labels)
df_synthesize = pd.DataFrame(infos, columns = ['content','label'])
print(df_synthesize)
print(list(infos))

我使用infos来初始化 Dataframe ,但是,在创建之后,信息变为空列表。

print(list(infos))

[]
很奇怪,为什么会这样?
Pandas版:1.1.5

nfg76nw0

nfg76nw01#

试试这个。1.1.5

import pandas as pd
sentences=['aaa','bbb','ccc']
labels = [1,2,3]
infos = list(zip(sentences , labels))
       // ^----------------------------------- clue
df_synthesize = pd.DataFrame(infos, columns = ['content','label'])
print(df_synthesize)

产出

content  label
0     aaa      1
1     bbb      2
2     ccc      3

相关问题