Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am new to python this is my code which I am working:

import pandas as pd

import statistics

import matplotlib.pyplot as plt

import math

from datetime import datetime

start_time = datetime.now()

gf = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/exfileCLEAN2.csv",

                 skiprows=[1])

bf = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/2SeconddatasetCLEAN.csv",

                 skiprows=[1])

df = (input("Which data set? "))

magnitudes = (df['Magnitude '].values)

times = df['Time '].values

average = statistics.mean(magnitudes)

sd = statistics.stdev(magnitudes)

below = sd * 3

class data_set:

    def __init__(self, index):

        self.mags = []

        self.i = index

        self.mid_time = df['Time '][index]

        self.mid_mag = df['Magnitude '][index]

        self.times = []

        ran = 80

        for ii in range(ran):

            self.times.append(df['Time '][self.i + ii - ran / 2])

            self.mags.append(df['Magnitude '][self.i + ii - ran / 2])

data = []

today = float(input("What is the range? "))

i = 0

while (i < len(df['Magnitude '])):

    if (abs(df['Magnitude '][i]) <= (average - below)):

        # check if neighbours

        t = df['Time '][i]

        tt = True

        for d in range(len(data)):

            if abs(t - data[d].mid_time) <= today:

                # check if closer to center

                if df['Magnitude '][i] < data[d].mid_mag:

                    data[d] = data_set(i)

                    print("here")

                tt = False

                break

        if tt:

            data.append(data_set(i))

    i += 1

print("found values")

# graphing

height = 2  # Change this for number of columns

width = math.ceil(len(data) / height)

if width < 2:

    width = 2

fig, axes = plt.subplots(width, height, figsize=(30, 30))

row = 0

col = 0

for i in range(len(data)):

    axes[row][col].plot(data[i].times, data[i].mags)

    col += 1

    if col > height - 1:

        col = 0

        row += 1

plt.show()

end_time = datetime.now()

print('Duration: {}'.format(end_time - start_time))

Currently, the error produced is this:

/Users/aaronhuang/.conda/envs/EXTTEst/bin/python "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py"

Which data set? gf

Traceback (most recent call last):

  File "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py", line 14, in <module>

    magnitudes = int(df['Magnitude '].values)

TypeError: string indices must be integers

Process finished with exit code 1

I am trying to have a user be able to choose which file to access to perform the rest of the code on.

So if the user types gf I wanted my code to access the first data file.

Any help would be appreciated. Thank you

1 Answer

0 votes
by (36.8k points)

Why not use an if-statement at the beginning? Try this:

instead of:

gf = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/exfileCLEAN2.csv",

                 skiprows=[1])

bf = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/2SeconddatasetCLEAN.csv",

                 skiprows=[1])

df = (input("Which data set? "))

Use this:

choice = input("Which data set? ")

if choice == "gf":

    df = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/exfileCLEAN2.csv",

                 skiprows=[1])

elif choice == "bf":

    df = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/2SeconddatasetCLEAN.csv",

                 skiprows=[1])

else:

    print("Error. Your choice is not valid")

    df = ""

    break

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch.

Browse Categories

...