简体   繁体   English

Pandas数据框的子集,其中包含具有特定列值的行

[英]Subset of a Pandas Dataframe consisting of rows with specific column values

I'm having a problem with a single line of my code. 我的单行代码有问题。 Here is what I'd like to achieve: 这是我想要实现的目标:

  • reading_now is a string consisting of 3 characters reading_now是由3个字符组成的字符串
  • df2 is a data frame that is a subset of df1 df2是作为df1子集的数据帧
  • I'd like df2 to consist of rows from df1 where the first three characters of the value in column "Code" is equal to "reading_now" 我希望df2由df1中的行组成,其中“代码”列中值的前三个字符等于“ reading_now”

I tried using the following two lines with no success: 我尝试使用以下两行没有成功:

*df2 = df1.loc[(df1['Code'])[0:3] == reading_now]*
*df2 = df1[(str(df1.Code)[0:3] == reading_now)]*

You could use 你可以用

df2 = df1[df1['Code'].str[0:3] == reading_now]

For example: 例如:

data = ['abcd', 'cbdz', 'abcz', 'bdaz']

df1 = pd.DataFrame(data, columns=['Code'])
df2 = df1[df1['Code'].str[0:3] == 'abc']

df2 will result in a dataframe with 'Code' column containing 'abcd' and 'abcz' df2将导致数据框的“代码”列包含“ abcd”和“ abcz”

Looks like you were really close with your 2nd attempt. 看起来您第二次尝试真的很接近。

You could solve this a couple of different ways. 您可以通过两种不同的方法解决此问题。

reading_now = 'AAA'
df1 = pd.DataFrame([{'Code': 'AAA'}, {'Code': 'BBB'}, {'Code': 'CCC'}])

solution : 解决方案

df2 = df1[df1['Code'].str.startswith(reading_now)]

or 要么

df2 = df1[df1['Code'][0:3] == reading_now]

The df2 dataframe will contain the row that starts with the reading_now string. df2数据帧将包含以reading_now字符串开头的行。

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

相关问题 Plotly:如何在列中的特定值处对 pandas dataframe 进行子集化? - Plotly: How to subset a pandas dataframe at specific values in a column? 仅对 pandas dataframe 中的特定行子集进行排序 - Sorting only specific subset of rows in pandas dataframe pandas的子集行dataframe上的字符串匹配 - subset rows of pandas dataframe by string match on column 根据列从 Pandas 数据框中选择数据,该列由列表中的值组成? - Select data from pandas dataframe based on a column, consisting of values in a list? Pandas DataFrame 根据特定逻辑识别共享列值的行 - Pandas DataFrame identifiying rows that share column values according to a specific logic 收集 pandas dataframe 中特定列值的所有行 - Collect all rows for specific column values in pandas dataframe 熊猫:仅当特定列中的值以开头时,才选择数据框行 - Pandas: select dataframe rows only if the values in a specific column start with 创建由符合条件的现有数据框的特定行组成的新熊猫数据框的最佳方法是什么? - What is the best way to create new pandas dataframe consisting of specific rows of an existing dataframe that match criteria? Pandas Dataframe - 按列值过滤 dataframe 行 - Pandas Dataframe - Filtering dataframe rows by column values 在Pandas DataFrame上更新行子集的列值的有效方法? - Efficient way to update column value for subset of rows on Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM