简体   繁体   English

如何附加带有列表的pandas Dataframe?

[英]How to append a pandas Dataframe with a list?

I have a list which I get after performing x=list(df.iloc[3]) the list which I get is 我有一个列表,我在执行x=list(df.iloc[3])得到的列表是我得到的

[u'some name',
 u'some product name',
 u'some url',
 u'Development',
 u'Web Development',
 u'MX2,250',
 14247,
 4160,
 nan]

I have to append this list into another dataframe named "new" with columns 我必须将此列表附加到另一个名为“new”的数据框中

Index(
    [u'Instructor Name', u'Product Name',
     u'Product Url', u'Category 1', u'Category 2',
     u'Price', u'Students Enrolled', u'Reviews', u'Stars'],
     dtype='object')

When I use new.append(x,ignore_index=True) I am getting a new column with the values of x in it which is not what I want. 当我使用new.append(x,ignore_index=True)我得到一个新的列,其中x的值不是我想要的。 How to flatten the list so values of x can be appended with the new. 如何展平列表,以便x的值可以附加新的。

append a series instead of a list like this 追加系列而不是像这样的列表

new.append(pd.Series(x, new.columns), ignore_index=True)

在此输入图像描述


This also works but only if new 's index is ordered sequential integers. 这也有效,但前提是new的索引是有序的整数。

new.loc[len(new), :] = x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM