Make a variable based on dataframe iterable rows which contain value from left column (2)

爱⌒轻易说出口 提交于 2020-12-15 04:56:48

问题


So, thanks for your appreciation to solve the code, but i think i use your code to have run similar way dataframe, but when i run original dataframe to perform a function, the problem occurs.

Here is my original dataframe

[In] : df
[Out]: 

    Keterangan                                                       Q2 2019        Q2 2018
0   Kas                                                              22686796.0     27421625.0
1   Giro pada bank indonesia                                         68409507.0     71159442.0
2   Giro pada bank lain                                              15675129.0     12584938.0
3   Giro pada bank lain pihak ketiga                                 88548.0        92417.0
4   Giro pada bank lain pihak berelasi                               41391653.0     84668151.0
5   Penempatan pada bank indonesia dan bank lain                     1825890.0      2349900.0
6   Penempatan pada bank indonesia dan bank lain pihak ketiga        28443695.0     30264303.0
7   Penempatan pada bank indonesia dan bank lain pihak berelasi      144798482.0    154020507.0
8   Efek-efek yang diperdagangkan                                    -758.0        -758.0
9   Efek-efek yang diperdagangkan pihak ketiga                       24081797.0     9396553.
10  Efek-efek yang diperdagangkan pihak berelasi                     20253524.0     20584035.0
11  Cadangan kerugian penurunan nilai pada efek-efek                 2713267.0      6858655.0

when i run your code it works smooth for make a column to be a variable, its need to become variable so you can run a def function right?

columns_1 = df['Keterangan']
columns_2 = df[columns_a] #here is unified column which i have been processed before

so working with your code, so i run it

def row_to_var(df, columns_1, columns_2, space_replace_with=''):
"""
columns_1 will be used as variable names and columns_2 as the values
"""
df_dict=dict(zip(df[columns_1].values,df[columns_2].values))
for key,val in df_dict.items():
  varname=str(key).replace(' ',space_replace_with)
  exec(varname + '='+str(val), globals())

row_to_var(df,'Keterangan', columns_a)

and its error which inform on google collab like this

 File "<string>", line 1
 SyntaxError: can't assign to operator

can you evaluate why it's happen which has similar type dataframe, but cant work properly in your way?

来源:https://stackoverflow.com/questions/64864719/make-a-variable-based-on-dataframe-iterable-rows-which-contain-value-from-left-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!