0

Ex:

A B C D
0 2 2 3
2 4 5 6
2 4 6 8

Here, the index of column "C" is 2. I want to store this index in the variable by converting that column name into variable.

So, C=2

Note this should work in dynamic case.

Tried:

exec("%s = %s" % "a","b")

But this is not working in a function.

The name of the column is unknown, need the index of the column stored in the same column name.

  • what's the data format: a plain text or a pandas dataframe? – RomanPerekhrest Jan 07 '23 at 16:51
  • You should not assign variables dynamically, this leads to bad quality code and potential vulnerabilities. Why are you trying to do this? Also should only the first row be used as values? If you have a pandas DataFrame: `df.iloc[0].to_dict()` will be a much better approach. – mozway Jan 07 '23 at 16:53
  • I have a list of columns like A,B,C,D. so name the variable name should be the column name only and it should hold the index of the column. – Kanakaraj B Jan 07 '23 at 17:14
  • `{k: v for v,k in enumerate(df.columns)}` if a pandas DataFrame (which is still unclear in your question) – mozway Jan 07 '23 at 18:25
  • Yes, it is a question on pandas dataframe. Tried - {k: v for v,k in enumerate(df.columns)}, it is giving me a dictionary with column name and its index. How can I convert those column names into variable which should be holding the index of that particular column. – Kanakaraj B Jan 08 '23 at 09:24

0 Answers0