0

I got a key error about calling a dataframe.

dist1=pd.read_csv(file1,sep="\s+")
dist1.astype(int)
dist1.columns.astype(int)

I call dist1 with no error. I try to call dist1[3][4] but it gives key error. Why? I couldn't find the solution. For my problem i need to call dist1[x][y] for further configuration. Where did it go wrong? Thanks.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
özgür Sanli
  • 103
  • 1
  • 8
  • 2
    For debugging help, you need to make a [mre] including example input, expected output, and the [full error message with traceback](https://meta.stackoverflow.com/q/359146/4518341). For Pandas-specific tips, see [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Feb 24 '22 at 22:26
  • Specifically, we need to see what indexes `dist1` has and what index is erroring. For example, maybe it doesn't have at least 4 columns and 5 rows. – wjandrea Feb 24 '22 at 22:31
  • I solve the problem,i convert the dataframe to numpy array. – özgür Sanli Feb 24 '22 at 22:36
  • Your question is too generic. As wjandrea said, we need more information, for example: how many columns, rows and leaves does your CSV file have and what kind of data are we talking about in each dataset? What exactly do you expect to access in dist1[3][4] ? Assuming you have a CSV file with only one sheet, which has N rows and M columns, I believe you want to access row 3 from column 4, am I right? If yes, you can try using `dist1.iloc[3][4]` – Maykon Meneghel Feb 24 '22 at 22:39
  • Thank all of you for your contribution.I don't want to work with iloc because of mix integer programming – özgür Sanli Feb 24 '22 at 22:40
  • You're right. It doesn't work with python-mip. However, you need to include the python-mib tag in the topic, to broaden the range of contributions on the topic. This can help people in the future. – Maykon Meneghel Feb 24 '22 at 22:46

1 Answers1

-1

I solve the problem by converting the dataframe to numpy

dist1=dist1.to_numpy()
özgür Sanli
  • 103
  • 1
  • 8
  • This makes it sound like you were trying to use numbered indexes on a labeled index. Why not just use `.iloc`? – wjandrea Feb 24 '22 at 22:40
  • It doesn't work with python-mip. – özgür Sanli Feb 24 '22 at 22:41
  • I'm not sure this is the best answer to your question. It might have resolved. But here you just converted your pandas dataframe structure to a numpy array and overridden your `dist1` variable. – Maykon Meneghel Feb 24 '22 at 22:43
  • @özgür Please edit the question to mention that. In the future, please avoid answering unclear questions, including your own. – wjandrea Feb 24 '22 at 22:46