Home > Blockchain >  How to access integer named attribute from pyspark row?
How to access integer named attribute from pyspark row?

Time:01-20

For the row object defined as

from pyspark.sql import Row
rr=Row('a',3)
r = rr(3,4)
print(r)
output:
Row(a=3, 3=4)

I can access the value of a as either r.a or r['a']

But neither can be used to access the value of the key 3. How can I access the value of a Row attribute if it is named by an integer?

CodePudding user response:

You can extract it by using its position within the row. So in your case, that would be the value at index 1:

>>> r[1]
4

Or, if you don't know the position on beforehand, and just want to index using the column name 3 you could do something like this:

# Grabbing index of column with 3 as name
>>> wantedIndex = r.__fields__.index(3)
>>> r[wantedIndex]
4
  • Related