Neha
Neha

Reputation: 27

Pandas dataframe

I have a dataframe df having two columns 'voltage'(v) and 'current'(I). I want to randomly select 5 values of 'voltage' from the file, save it in 1D array like [v1,v2,v3,v4,v5], and save the corresponding values of currents in another 1D array like [I1,I2,...,I5]. Here is what I tried:

 df=pd.read_csv(file,sep=",",header=None,usecols=[0,1],names=['voltage','current'])

#pick 5 random values of voltage and save it in np array
 V= np.array( df['voltage'].sample(n=5))

How to do the same with the corresponding values of I at selected values of V?

Upvotes: 1

Views: 44

Answers (2)

Yuca
Yuca

Reputation: 6101

While jezrael's answer does provide the desired output, the answer to your question would be:

V= df['voltage'].sample(n=5)
I = df.loc[V.index,'current']

Upvotes: 0

jezrael
jezrael

Reputation: 863401

I think need:

arr = df.sample(n=5).values
a = arr[:, 0]
b = arr[:, 1]

Upvotes: 1

Related Questions