当 pandas.dataframe.values 切片工作时,从 pandas.dataframe.iloc 切片制作列表不起作用

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

Making lists from pandas.dataframe.iloc slice doesn't work while pandas.dataframe.values slice work

pythonpandasslice

提问by Ando Jurai

I want to agregate columns from a dataframe into a new columns

我想将数据框中的列聚合到新列中

So I used:

所以我使用了:

newdataframe["agregate1"]=list(df.iloc[:,0:4])

but it doesn't work. It returns an ["intensity_x"] list (with x as column index), these strings being the names of my dataframe columns.

但它不起作用。它返回一个 ["intensity_x"] 列表(x 作为列索引),这些字符串是我的数据框列的名称。

While

尽管

newdataframe["agregate1"]=list(df.iloc[0,0:4])

will return data such as

将返回数据,如

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2920000000.0, 2830000000.0, 3030000000.0, 0.0, 2980000000.0, 3000000000.0].

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2920000000.0, 2830000000.0, 3030000000.0, 0.0, 2980000000.0, 3000000]

It is actually important since I can't retrieve correctly my data and it seems like a bug, or I am missing something important.

这实际上很重要,因为我无法正确检索我的数据,这似乎是一个错误,或者我遗漏了一些重要的东西。

By reading: pack dataframe columns to list in pandas

通过阅读: pack dataframe columns to list in pandas

I circumvented the problem by using df.values instead of iloc, still I don't understand what is the problem with using "whole slices", only getting headers instead. This is strange. Thanks

我通过使用 df.values 而不是 iloc 规避了这个问题,但我仍然不明白使用“整个切片”有什么问题,只得到标题。这很奇怪。谢谢

采纳答案by jezrael

If use:

如果使用:

df1 = df.iloc[:,0:4]

output is DataFrame.

输出是DataFrame

If:

如果:

df2 = df.iloc[0,0:4]

Output is Series- first rowin dfand columns 1:4.

输出是Series-first rowindf和列1:4

Sample:

样本:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

df1 = df.iloc[:,0:4]
print (df1)
   A  B  C  D
0  1  4  7  1
1  2  5  8  3
2  3  6  9  5

df2 = df.iloc[0,0:4]
print (df2)
A    1
B    4
C    7
D    1
Name: 0, dtype: int64

Then you can use valuesif need output as numpy array:

然后你可以使用valuesif 需要输出为numpy array

print (df1.values)
[[1 4 7 1]
 [2 5 8 3]
 [3 6 9 5]]

If need output listof lists, use:

如果需要输出listlists,使用:

print (df1.values.tolist())
[[1, 4, 7, 1], [2, 5, 8, 3], [3, 6, 9, 5]]

If need list in new column:

如果需要在新列中列出:

df['new'] = df.iloc[:,0:4].values.tolist()
print (df)
   A  B  C  D  E  F           new
0  1  4  7  1  5  7  [1, 4, 7, 1]
1  2  5  8  3  3  4  [2, 5, 8, 3]
2  3  6  9  5  6  3  [3, 6, 9, 5]

df['new'] = list(df.iloc[:,0:4].values)
print (df)
   A  B  C  D  E  F           new
0  1  4  7  1  5  7  [1, 4, 7, 1]
1  2  5  8  3  3  4  [2, 5, 8, 3]
2  3  6  9  5  6  3  [3, 6, 9, 5]