0

I want to standardize the score in each group. After I scaled the data, it returned a series. I cannot create a new column in my data frame and put results in it.

The result I got is like :

  • 3M Company [[1.0], [1.0], [0.9]]
  • A.O. Smith Corp [[1.0], [0.85]]
  • AES Corp [[1.0], [0.5]]

result I got

The result I want is like:

  • [1.0, 1.0, 0.9, 1.0, 0.85, 1.0, 0.5]

In that case, I can put it into new column in data frame. Can anyone help me? Thank you!!

bjschoenfeld
  • 402
  • 6
  • 14
Min Sun
  • 7
  • 1
  • 5
  • You should post a small sample of your data frame that others can work off of. Right now this leaves a lot to interpretation and any solutions might not work for your use case. Also what have you tried already? What gave you the result you got currently? – sedavidw Aug 27 '19 at 19:21
  • Thanks for your advise. I tried to upload a picture or table but failed. I will try again. – Min Sun Aug 27 '19 at 19:31
  • It looks like you have a Series where each item is indexed by the name of a company and the value is a list of lists of size 1. Is that correct? – bjschoenfeld Aug 27 '19 at 19:49
  • @brandon Yes. I think that's my situation here. – Min Sun Aug 27 '19 at 20:04

2 Answers2

0

You have a series of lists of lists that you want to flatten. This is related to this question, except that you have one more level of nested lists.

You will have to iterate over each item in the Series and flatten them. Using list comprehension:

dd = pandas.Series(
    data=[
        [[1.0], [1.0], [0.9]],
        [[1.0], [0.85]],
        [[1.0], [0.5]]
    ],
    index=['3M Company', 'A.O. Smith Corp', 'AES Corp']
)
flattened = [item[0] for row in dd for item in row]

which results in

[1.0, 1.0, 0.9, 1.0, 0.85, 1.0, 0.5]
bjschoenfeld
  • 402
  • 6
  • 14
  • Thank you for your answer. But the size of my data is big. I just list part of it in order to illustrate. Also, the result I got is like `array[array[[1.0],[1.0]]]`. I just upload a picture. I think I also need to fix the '[]' outside each number. – Min Sun Aug 27 '19 at 19:42
0
Sum_array = []
ThreeM = ['1.0', '1.0', '0.9']
AO =  ['1.0', '0.85']
AES =  ['1.0', '0.5']
for x in range(0, ThreeM.__len__()):
    Sum_array.append(ThreeM[x])

for x in range(0, AO.__len__()):
    Sum_array.append(AO[x])

for x in range(0, AES.__len__()):
    Sum_array.append(AES[x])

print(Sum_array)
Puértolas Luis
  • 235
  • 1
  • 2
  • 13