gpt4 book ai didi

python-2.7 - 将元组附加到 Pandas DataFrame

转载 作者:行者123 更新时间:2023-12-04 20:11:00 27 4
gpt4 key购买 nike

我正在尝试加入(垂直)一些元组,最好说我将这些元组插入到数据框中。但直到现在还做不到。问题出现了,因为我试图水平而不是垂直添加它们。

data_frame = pandas.DataFrame(columns=("A","B","C","D"))
str1 = "Doodles are the logo-incorporating works of art that Google regularly features on its homepage. They began in 1998 with a stick figure by Google co-founders Larry Page and Sergey Brin -- to indicate they were attending the Burning Man festival. Since then the doodles have become works of art -- some of them high-tech and complex -- created by a team of doodlers. Stay tuned here for more of this year's doodles"

aa = str1.split()
bb = zip(aa[0:4])

data_frame.append(bb,ignore_index=True,verify_integrity=False)

是否可能或我必须遍历元组中的每个单词才能使用 插入

最佳答案

你可以这样做

In [8]: index=list('ABCD')

In [9]: df = pd.DataFrame(columns=index)

In [11]: df.append(Series(aa[0:4],index=index),ignore_index=True)
Out[11]:
A B C D
0 Doodles are the logo-incorporating

或者,如果您有许多要追加的行,只需
创建一个列表,然后 DataFame(list_of_series)在末尾
In [13]: DataFrame([ aa[0:4], aa[5:8] ],columns=list('ABCD'))
Out[13]:
A B C D
0 Doodles are the logo-incorporating
1 of art that None

关于python-2.7 - 将元组附加到 Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243846/

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