import pandas as pd
from random import random
from collections import namedtuple
Smoker = namedtuple("Smoker", ["Female","Male"])
Nonsmoker = namedtuple("Nonsmoker", ["Female","Male"])
DF = dict()
DF["A"] = [(Smoker(random(),random()), Nonsmoker(random(),random())) for t in range(3)]
DF["B"] = [(Smoker(random(),random()), Nonsmoker(random(),random())) for t in range(3)]
DF = pd.DataFrame(DF, index=["t="+str(t+1) for t in range(3)])
我有这个数据框,其中每个单元格都是两个命名元组的元组。将其保存到 csv 文件并重新加载后,打印输出看起来相同,但每个单元格都变成了string。它是怎么发生的?我应该怎么做才能每次都获得相同的数据帧?
DF.to_csv("results.csv", index_label=False)
df = pd.read_csv('results.csv', index_col=0)
print(df)
for a,b in zip(df.A,df.B):
print(type(a),type(b))
大话西游666
胡说叔叔
相关分类