gpt4 book ai didi

python - 如何将列表连接到 Pandas 中的数据框

转载 作者:行者123 更新时间:2023-12-01 04:37:08 24 4
gpt4 key购买 nike

c1=["q","q","q","q","q","q"]
c2=["x","x","x","x","x","x"]
c3=["w","w","w","w","w","w"]
ca=["c","e","a","d"]
cb=["y","z","s","f"]
cc=["y","z","s","f"]
df1=pd.DataFrame(c1, columns=['c1'])
df2=pd.DataFrame(c2, columns=['c2'])
df3=pd.DataFrame(c3, columns=['c3'])
df4=pd.DataFrame(ca, columns=['ca'])
df5=pd.DataFrame(cb, columns=['cb'])
df6=pd.DataFrame(cc, columns=['cc'])
df7=pd.concat([df1,df2,df3,df4,df5,df6],axis=1)
df7

我想要做的是连接列表(不同长度)并制作数据框。我无法使用 zip()s 实现它。有什么简单的方法吗?

最佳答案

您可以喂concat使用系列列表而不是数据框列表。对于可变数量的变量,字典是一个好主意,它允许您将 future 的列名存储为键。

d = {'c1': c1, 'c2': c2, 'c3': c3, 'ca': ca, 'cb': cb, 'cc': cc}

df = pd.concat([pd.Series(v, name=k) for k, v in d.items()], axis=1)

print(df)

c1 c2 c3 ca cb cc
0 q x w c y y
1 q x w e z z
2 q x w a s s
3 q x w d f f
4 q x w NaN NaN NaN
5 q x w NaN NaN NaN

关于python - 如何将列表连接到 Pandas 中的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51570405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com