pandas 附加列表作为数据框行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53500735/
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-14 06:10:42  来源:igfitidea点击:

appending a list as dataframe row

pandaspython-3.5

提问by HT121

I have a list with some counts with 5 elements.

我有一个包含 5 个元素的计数的列表。

counts = [33, 35, 17, 38, 29]

This counts list is updated with new numbers every day. So I wanted to create a dataframe and append the counts data as a new row every day. Every element of the list should appear in separate column in the dataframe. What i want to do is the following:

这个计数列表每天都会更新新的数字。所以我想创建一个数据框并每天将计数数据附加为一个新行。列表中的每个元素都应出现在数据框中的单独列中。我想要做的是以下内容:

df = pd.DataFrame(columns = ['w1', 'w2', 'w3', 'w4', 'w5'])
df = df.append(counts)

but instead of adding counts data as a row, it adds a new column. Any help on how to do this correctly?

但不是将计数数据添加为一行,而是添加一个新列。有关如何正确执行此操作的任何帮助?

Assume counts on day0 is [33, 35, 17, 38, 29] and on day1 is [30, 36, 20, 34, 32], what i want is the following as output:

假设第 0 天的计数是 [33, 35, 17, 38, 29] 并且第 1 天的计数是 [30, 36, 20, 34, 32],我想要的是以下输出:

      w1 w2 w3 w4 w5
    0 33 35 17 38 29
    1 30 36 20 34 32

where index represent the day at which counts were taken. any help?

其中 index 表示进行计数的日期。有什么帮助吗?

回答by jezrael

Appending to DataFrame is possible, but because slow if many rows, better is create list of lists and create DataFrameby contructor:

附加到 DataFrame 是可能的,但因为如果多行会很慢,最好创建列表列表DataFrame并由构造函数创建:

counts = [33, 35, 17, 38, 29]
counts1 = [37, 8, 1, 2, 0]

L = [counts, counts1]
df = pd.DataFrame(L, columns = ['w1', 'w2', 'w3', 'w4', 'w5'])
print (df)

   w1  w2  w3  w4  w5
0  33  35  17  38  29
1  37   8   1   2   0

But if need it, e.g. appenf one row daily only, then is necessary create Serieswith same index values like columns:

但是如果需要它,例如每天只追加一行,那么有必要Series使用相同的索引值创建,例如columns

df = pd.DataFrame(columns = ['w1', 'w2', 'w3', 'w4', 'w5'])

counts = [33, 35, 17, 38, 29]

s = pd.Series(counts, index=df.columns)
df = df.append(s, ignore_index=True)
print (df)
   w1  w2  w3  w4  w5
0  33  35  17  38  29

回答by piRSquared

If you know day0and day1in the beginning, then you want to construct it as jezrael suggests.

如果你知道day0,并day1在开始的时候,那么你想为jezrael提出构建它。

ButI'm assuming that you want to be able to add a new row in a loop.

我假设您希望能够在循环中添加新行。

Solution with loc

解决方案 loc

When using locyou need to use an index value that doesn't already exist. In this case, I'm assuming that we are maintaining a generic RangeIndex. If that is the case, I'll assume that the next index is the same as the length of the current DataFrame.

使用时,loc您需要使用尚不存在的索引值。在这种情况下,我假设我们正在维护一个通用的RangeIndex. 如果是这种情况,我将假设下一个索引与当前 DataFrame 的长度相同。

df.loc[len(df), :] = counts
df

   w1  w2  w3  w4  w5
0  33  35  17  38  29

Let's make the loop.

让我们制作循环。

day0 = [33, 35, 17, 38, 29]
day1 = [30, 36, 20, 34, 32]

for counts in [day0, day1]:
  df.loc[len(df), :] = counts

df

     w1    w2    w3    w4    w5
0  33.0  35.0  17.0  38.0  29.0
1  30.0  36.0  20.0  34.0  32.0