简体   繁体   English

如何使用列索引删除第一列中以特定字符串开头的 Dataframe 行

[英]How to drop Dataframe rows that start with a certain character string in the first column using column index

I am transposing a data frame where I do not have defined column names and then need to drop rows from the transposed table where a given rows value in the first column (index 0) starts with 'zrx'.我正在转置一个没有定义列名的数据框,然后需要从转置表中删除行,其中第一列(索引 0)中的给定行值以“zrx”开头。 I am thinking something like this should work, but can't seem to get it working:我在想这样的事情应该有效,但似乎无法正常工作:

df[~df[0].str.startswitg("zrx")]

Input data looks like this (no headers):输入数据如下所示(无标题):

Index 0    Index 1
zrx456.       True
zrx567        false
abc234      True
Gfh123       False
nbv345       True
zrx456         False
zrx668        True
zrx789         True

My goal is to return this data frame with only the rows that start with zrx in column 0.我的目标是返回此数据框,其中仅包含第 0 列中以 zrx 开头的行。

If you know the name of the first column, use如果您知道第一列的名称,请使用

df[~df.Artist.str.startswith('zrx')]

If you do not know the name of the first column, use如果您不知道第一列的名称,请使用

df[~df.iloc[:,0].str.startswith('zrx')]

input输入

Artist  Album   Point
0   zrxAC1  A   1
1   AC2     B   2
2   zrxAC1  NaN     3
3   AC4     A   4
4   AC5     C   5

Output输出

Artist  Album   Point
1   AC2     B   2
3   AC4     A   4
4   AC5     C   5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除Pandas DataFrame某列值为NaN的行 - How to drop rows of Pandas DataFrame whose value in a certain column is NaN 如何删除DataFrame的索引列? - How to drop the index column of DataFrame? 如何在dataframe列中的某些字符后提取整个字符串部分? - How to extract entire part of string after certain character in dataframe column? 如果列匹配特定字符串,则在数据框中删除行 - Drop rows in dataframe if the column matches particular string 如何在不引用列名的情况下删除第一列包含特定字符的所有行? - How to drop all rows where the first column contains a specific character without referencing column name? 使用 Pandas Dataframe,如何拆分特定列中的字符串,然后用拆分的第一个索引替换该字符串? - Using a Pandas Dataframe, how can I split the strings in a specific column and then replace that string with the first index of the split? 如何在熊猫数据框中的列中删除带有“nan”的行? - how to drop rows with 'nan' in a column in a pandas dataframe? 如何通过列值的条件删除 DataFrame 中的行 - How to Drop rows in DataFrame by conditions on column values DataFrame删除其列具有特定值的行 - DataFrame drop rows whose column has certain values Excel 到 Pandas DataFrame 使用第一列作为索引 - Excel to Pandas DataFrame using first column as index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM