1

I had a problem with for loops earlier, and it was solved thanks to @mak4515, however, there is something else I want to accomplish

# Use pandas to read in csv file
data_df_0 = pd.read_csv('puget_sound_ctd.csv')
#create data subsets based on specific buoy coordinates
data_df_1 = pd.read_csv('puget_sound_ctd.csv', skiprows=range(9,114))
data_df_2 = pd.read_csv('puget_sound_ctd.csv', skiprows=([i for i in range(1, 8)] + [j for j in range(21, 114)]))
for x in range(0,2):
    for df in [data_df_0, data_df_2]:
        lon_(x) = df['longitude']
        lat_(x) = df['latitude']

This is my current code, I want to have it have it so that it reads the different data sets and creates different values based on the data set it is reading. However, when I run the code this way I get this error

  File "<ipython-input-66-446aebc48604>", line 21
    lon_(x) = df['longitude']
    ^
SyntaxError: can't assign to function call

What does "can't assign to function call" mean, and how do I fix this?

Mark Antony
  • 41
  • 1
  • 3
  • Try `lon_[x] = df['longitude']` ? – RMPR Apr 08 '20 at 15:08
  • 3
    you cannot assign variables like that. The best approach would be to create them as keys in a dict – It_is_Chris Apr 08 '20 at 15:10
  • 1
    See https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – ALollz Apr 08 '20 at 15:23
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – wwii Apr 08 '20 at 16:03

1 Answers1

0

I think the comment by @Chris is probably a good way to go. I wanted to point out that since you're already using pandas dataframes, an easier way might be to make a column corresponding to the original dataframe then concatenate them.

import pandas as pd
data_df_0 = pd.DataFrame({'longitude':range(-125,-120,1),'latitude':range(45,50,1)})
data_df_0['dfi'] = 0
data_df_2 = pd.DataFrame({'longitude':range(-120,-125,-1),'latitude':range(50,45,-1),'dfi':[2]*5})
data_df_2['dfi'] = 2

df = pd.concat([data_df_0,data_df_2])

Then, you could acess data from the original frames like this:

df.loc[2]
EMiller
  • 817
  • 1
  • 7
  • 20