if statement simple data processing

Asked 2 years ago, Updated 2 years ago, 351 views

I'm a beginner, so I may not be able to say enough, but I appreciate your cooperation.

The csv file (filename "data-path") says the following in order from the first row to the first column.
I was trying to write a program that looked at the first line and found a number above 1.0 (in this case, 1.5), deleted the previous data (in lines 1-4) and saved it in csv, but it didn't work, so I asked. I was going to write it using an if statement.

CSV file examples:

0.1
0.3
0.2
0.9
1.5
0.8
1.1
hereinafter abbreviated
import numpy as np
import pandas aspd
from pandas import Series, DataFrame
import matplotlib.pyplot asplt
%matplotlib inline

data_path_A = '201016 timesi.csv'

# Loading Data
data_all_A = pd.read_csv(data_path_A)
data_all_A = np.array(data_all_A)

for i in range (90000):
    if data_all_A[i]>0.1:# Reference value is 0.03
         data_all_A = np.delete(data_all_A, i, 0)
    else:
        break
        
print(data_all_A)

python

2022-09-30 21:53

2 Answers

I think using dropwhile() is the easiest.

from ittertools import dropwhile

data = (0.1, 0.3, 0.2, 0.9, 1.5, 0.8, 1.1)

for x in (dropwhile (lambday:y<1.0, data)) :
    print(x)

1.5
0.8
1.1

https://docs.python.org/ja/3/library/itertools.html#itertools.dropwhile

Supplemental (editing)
If you delete the data in the array referenced in the loop, the index of the elements will be shifted, so you shouldn't do it basically (not even recommended for cost).You should create a new array.


2022-09-30 21:53

Use pandas.DataFrame.idxmax to determine the first line of 1.0 or higher.

import pandas as pd

data_path_A = '201016 timesi.csv'

data_all_A=pd.read_csv(data_path_A, header=None)
data_all_A=(
  data_all_A.loc [data_all_A[0].ge(1.0).idxmax():]
  .reset_index(drop=True)
)
print(data_all_A)

=>
     0
0  1.5
1  0.8
2  1.1


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.