I have a few columns
df = pd.read_csv('D:\data_ana\\10.05.43.csv')
cols_in_the_slice = df.loc[:,(' Objects[0].MeasuredTimeStamp',
' Objects[0].LifeCycles',
' Objects[0].KinematicRel.f_DistX',
' Objects[0].KinematicRel.f_DistY',
' Objects[0].KinematicRel.f_VrelX',
' Objects[0].KinematicRel.f_VrelY',
' Objects[0].KinematicAbs.f_VabsX',
'Objects[0].KinematicAbs.f_VabsY',
'Objects[0].SensorSpecific.f_RCS',
'Objects[0].CurrentTarget.f_DistX',
' Objects[0].CurrentTarget.f_DistY',
'Objects[0].CurrentTarget.f_VrelX',
'Objects[0].CurrentTarget.f_VrelY',
'Objects[0].CurrentTarget.f_RCS')].columns
I have these columns here I am manually entering he number for each column for eg If I want to enter the number 1 , then I have to change manually from Objects[0].MeasuredTimeStamp', ' Objects[0].LifeCycles' to Objects[1].MeasuredTimeStamp', ' Objects[1].LifeCycles' so on...
How can I automate this giving a random number to first column and it will be same for other column
CodePudding user response:
As you are working with strings, you can use f-strings. Below is an example. Note the ...
should be replaced with all the other columns:
index = 3
cols_in_the_slice = df.loc[:,
(f'Objects[{index}].MeasuredTimeStamp',
...,
f'Objects[{index}].CurrentTarget.f_RCS')].columns
As you have stated that you wish to assign a random number, you can set the index using the randint
function of the random
module.
You can learn more about this here: generating pseudo-random numbers in Python.
You will need to specify the range for the random number. Here is an example of how to generate an index between 0 and 10 (inclusive):
import random
index = random.randint(0, 10)