【问题标题】:Drop rows from a dataframe with a non-numeric index从具有非数字索引的数据框中删除行
【发布时间】:2017-09-18 00:40:11
【问题描述】:

我一直在使用 pandas 对 CSV 文件进行一些有趣的过滤,但遇到了障碍。我正在尝试检查我的索引列是否有乱码文本(非整数)数据,并删除这些行。我尝试在使用条件导入时将它们从数据框中删除,然后我尝试将它们迭代出来但没有成功。这是一个例子:

df = pd.read_csv(file, encoding='cp1252').set_index("numbers")
results = df[df["columnA"].str.contains("search_data") & ~df["columnB"].isin(seach_list)]
#I need to add to the above statement to check column "numbers" which I have set to be the index,
#to catch some expected garbled text and filter it out... because it is
#an integer, I can't use str.contains or isdigit or isalnum, I've tried to do len(df["columns"] < 20 , df.index < 20 .... i've tried 
#i've tried a few other options at this point as well
# after bringing it in, I've also tried iterating through it:
#
for index, row in results.iterrows():
    if not (isinstance( row["numbers"], int )):
         print(str(row["numbers"]))
         #append whole row to new dataframe
#This also didn't work   

对我能做什么有什么想法吗?

Example data in the "numbers columns = 329381432
Example garbled text in "numbers" column that I am 
trying to keep from importing: äu$ÒÔ”5$ò"Â$”äu$ÒÔ”5$ò 

附带说明一下,我必须更改 pd 函数的编码,以便当有一些非 utf-8 数据时,我仍然可以读取文件中的所有良好数据...否则会引发错误导入。

【问题讨论】:

    标签: python pandas dataframe indexing numeric


    【解决方案1】:

    您可以使用pd.to_numeric 将您的numbers 列转换为数字。所有非数字条目将被强制转换为NaN,然后您可以删除这些行。

    df = pd.read_csv(file, encoding='cp1252')
    df['numbers'] = pd.to_numeric(df['numbers'], errors='coerce')
    
    df = df.dropna(subset=['numbers']).set_index('numbers')
    

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 2015-04-22
      • 2021-07-22
      相关资源
      最近更新 更多