简体   繁体   English

我如何并排添加列表(而不是通过使用追加)?

[英]How can I add list side by side (not by using append)?

I need to add lists in side ways, not in the below. 我需要以侧面方式添加列表,而不是下面的方式。 So I think 'append()' is not appropriate. 所以我认为'append()'是不合适的。 'concat()' seems to be adding in side ways, but not working for lists. 'concat()'似乎是从侧面添加的,但不适用于列表。

Do I need to use concat() to add list side ways? 我是否需要使用concat()添加列表边方式? If so, how can I do that with lists? 如果是这样,我该如何使用列表?

months = range(1,3)

pieces=[]
columns = ['id','q1','q2','q3']

for month in months:
    path = 'C:/Users/uib57309/Desktop/newfolder/01_Survey/month/%d.csv' %month
    frame = pd.read_csv(path, names = columns)

    frame['month'] = month
    pieces.append(frame)

names = pd.concat(pieces)
print(names)

I used the append(), so the list was added in the below. 我使用了append(),因此列表添加在下面。

You can use .extend 您可以使用.extend

eg: 例如:

a = [1,2,3,4]
b = [5,6,7,8]

a.append(b)
print(a)
[1,2,3,4,[5,6,7,8]]

a.extend(b)
print(a)
[1,2,3,4,5,6,7,8]

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

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