将元组附加到 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17243846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:56:45  来源:igfitidea点击:

Appending Tuples to Pandas DataFrame

python-2.7pandas

提问by LonelySoul

I am trying to join (vertically) some of the tuples, better to say I am inserting these tuples in the dataframe. But unable to do so till now. Problem arises since I am trying to add them horizentally and not vertically.

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

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) 

Is it possible or do I have to iterate over each word in tuple to use insert

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

采纳答案by Jeff

You could do this

你可以这样做

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

alternatively if you have many of these rows that you are going to append, just create a list, then DataFame(list_of_series)at the end

或者,如果您有许多要追加的行,只需创建一个列表,然后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