Home > Software engineering >  Convert a dataframe array item to a dataframe column
Convert a dataframe array item to a dataframe column

Time:09-20

I have a dataframe that was filled from a json response. I can flatten the response to be a single row dataframe, but I'm struggling to determine the most efficient way to then map an dataframe object that is an array of numbers to another dataframe column.

I can make it work by using a temporary dataframe, but I'm sure there's a single line way to make this work.

print(dfjson['outputs.ac'])

Output:
0    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 314.1...

This code does what I want, but I don't like that I have to use a "temp" dataframe.

dftemp = dfjson.loc[:,"outputs.ac"]

df['%SolarGen']=dftemp.iloc[0]
print(df['%SolarGen'])

Output:
Name: outputs.ac, dtype: object
0       0.0
1       0.0
2       0.0
3       0.0
4       0.0

8755    0.0
8756    0.0
8757    0.0
8758    0.0
8759    0.0
Name: %SolarGen, Length: 8760, dtype: float64

When I use df['%SolarGen']=dfjson.loc[:,"outputs.ac"] it puts the array into index 0 of the %SolarGen column.

What am I missing? Thanks in advance.

CodePudding user response:

I found a single line solution df['%SolarGen']=dfjson.iloc[0,30] where 30 is the column number in the original json dataframe. I guess the next advance is to find the column number by name so I don't have to count them or in case anything changes.

  • Related