0

I need to extract the specified characters in the specified column in the csv file,I got an error saying "TypeError: tuple indices must be integers or slices, not str"

my csv file

name,age,sex
li,26,M
mu,30,F
import os
import pandas as pd
import glob
for i in glob.glob('H:\\1\\*.csv'):
    result_1=pd.read_csv(i,encoding="utf-8",dtype=object)
    SubNetwork_list=list(result_1.groupby(["name"]))
    for SubNetwork in SubNetwork_list:
        SubNetwork_pd=pd.DataFrame(SubNetwork["li"])
        SubNetwork_pd.to_csv("H:\\2\\li.csv",encoding="utf-8",
                         header=True,index=False,mode='a')

console error

SubNetwork_pd=pd.DataFrame(SubNetwork['li'])
TypeError: tuple indices must be integers or slices, not str

lex9527
  • 5
  • 3

1 Answers1

0

When iterating over a groupby, each element of that iterator is a tuple of (group_name, group_dataframe).

So to iterate over a groupby, you need to do something like this:

    SubNetwork_list=list(result_1.groupby(["name"]))
    for SubNetwork_name, SubNetwork in SubNetwork_list:

This unpacks the tuple.

More information: Docs

PS: You don't need to call list() on the groupby object. You can iterate over it without doing that, and it's faster.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • ` raise KeyError(key) from err KeyError: 'li'` – lex9527 Jul 12 '22 at 03:39
  • i get an error,can you provide the full code – lex9527 Jul 12 '22 at 03:40
  • I can't provide the full code - you haven't posted a [reproducible example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). The issue you're facing is caused by there being no 'li' column present in your input CSV. See [Indexing and selecting data tutorial](https://pandas.pydata.org/docs/user_guide/indexing.html#different-choices-for-indexing) and [DataFrame.set_index()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html) for how to do what you want. – Nick ODell Jul 12 '22 at 15:07