简体   繁体   English

将相同的列表添加到新列中的pandas DataFrame中的每一行

[英]Adding the same list to each row in a pandas DataFrame in a new column

In a pandas DataFrame, foo for example: 在pandas DataFrame中, foo例如:

>>> foo
   col1  col2
0     1     0
1     2     0
2     3     1

If you want to add a new column, all with the same value you can do 如果要添加新列,则可以使用相同的值

>>> foo['col3'] = 1
>>> foo
   col1  col2  col3
0     1     0     1
1     2     0     1
2     3     1     1

If you want to add another new column, all with specific values you can do 如果要添加另一个新列,则可以使用特定值

>>> foo['col4'] = ['a', 'b', 'c']
>>> foo
   col1  col2  col3  col4
0     1     0     1     a
1     2     0     1     b
2     3     1     1     c

But what I want to do is add the same list to each row as new column. 但我想要做的是将相同的列表添加到每一行作为新列。 Something like 就像是

>>> myList = [True, False, False, True]
>>> foo['col5'] = {...something something something...}
>>> foo
   col1  col2  col3  col4                        col5
0     1     0     1     a  [True, False, False, True]
1     2     0     1     b  [True, False, False, True]
2     3     1     1     c  [True, False, False, True]

Using the previous method results in ValueError('Length of values does not match length of ' 'index') . 使用前面的方法会导致ValueError('Length of values does not match length of ' 'index') So at the moment, my {...something something something...} line is foo['col5'] = [myList] * foo.shape[0] . 所以此刻,我的{...something something something...}行是foo['col5'] = [myList] * foo.shape[0] But I'm wondering, is there a better way? 但我想知道,有更好的方法吗?

Use a list comprehension. 使用列表理解。

v = [True, False, False, True]
df['col5'] = [v for _ in range(len(df))]

df
   col1  col2                        col5
0     1     0  [True, False, False, True]
1     2     0  [True, False, False, True]
2     3     1  [True, False, False, True]

You might be tempted to use 你可能很想使用它

df['col5'] = [True, False, False, True] * len(df)

However, each record actually references the same list. 但是,每条记录实际上都引用了相同的列表。 Try this - 尝试这个 -

df.loc[0, 'col5'][0] = False
df

   col1  col2                         col5
0     1     0  [False, False, False, True]
1     2     0  [False, False, False, True]
2     3     1  [False, False, False, True]

You'll see the change is reflected across all sublists. 您会看到更改会反映在所有子列表中。

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

相关问题 将新的dataFrame列添加到pandas中的相同数据框 - Adding new dataFrame column to the same dataframe in pandas 将列表作为行索引和列索引添加到pandas数据帧中 - Adding a list as row and column indices to pandas dataframe 在熊猫数据框中的新列中添加列表 - adding list in a new column in a pandas dataframe 将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值 - Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)? 为每一行运行一个函数并创建一个新的 Column Pandas Dataframe - Run a function for each row and create a new Column Pandas Dataframe 通过将每一行转换为 pandas dataframe 中的字典来创建新列 - Make new column by turning each row into a dict in a pandas dataframe 仅当行和列值相同时才添加两个pandas数据帧值 - adding two pandas dataframe value only if the row and column value is the same 迭代循环并将列表添加到新行或新列中的数据框 - Iterate over loop and adding list to dataframe in new row or new column 将每个 pandas 行与列表字典和 append 新变量与 dataframe 进行比较 - Compare each pandas row to a dictionary of list and append new variable to the dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM