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.
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].
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
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
If use:
df1 = df.iloc[:,0:4]
output is DataFrame.
If:
df2 = df.iloc[0,0:4]
Output is Series - first row in df and columns 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 values if need output as numpy array:
print (df1.values)
[[1 4 7 1]
[2 5 8 3]
[3 6 9 5]]
If need output list of lists, use:
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With