You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中使用Pandas与Series进行数据集挖掘的技术问题咨询

Hey there! Let's work through your two pandas questions step by step:

1. Finding the Major for the Student with FIDN = 30

Your current code df_test.get_value(30, 'Major') might work in some cases, but there are better (and more future-proof) ways to do this, plus we need to clarify a key detail about your DataFrame structure:

  • If FIDN is your row index (i.e., the index labels are the FIDN values):
    The get_value() method is actually deprecated in newer pandas versions (since 0.21.0). Instead, use .loc[] or .at[]—these are more explicit and widely recommended:

    # Using loc (works for single values or ranges)
    df_test.loc[30, 'Major']
    # Using at (faster for single value lookups)
    df_test.at[30, 'Major']
    
  • If FIDN is a regular column (not the index):
    Your current approach won't work because get_value() uses position-based indexing, not column values. Instead, filter the DataFrame to find rows where FIDN equals 30, then extract the Major:

    # Returns a Series with the matching Major(s)
    df_test.loc[df_test['FIDN'] == 30, 'Major']
    # If you know there's exactly one match, get the single value
    df_test.loc[df_test['FIDN'] == 30, 'Major'].iloc[0]
    
2. Sorting Records by LastName, then Tuition

Your code df_test.sort_values( ['LastName', 'tuition'] ) has the right syntax, but the issue is likely one of two things:

Reason 1: You're not capturing the sorted result

By default, sort_values() returns a new sorted DataFrame instead of modifying the original one. If you just run the line without assigning it to a variable or using inplace=True, you might only see a truncated preview (which could look like just column names/index). Fix this by either:

# Option 1: Assign to a new variable
sorted_students = df_test.sort_values( ['LastName', 'tuition'] )
# Now view the sorted data
print(sorted_students)
# Option 2: Modify the original DataFrame in-place
df_test.sort_values( ['LastName', 'tuition'], inplace=True )

Reason 2: Tuition is stored as a string (not a number)

If your tuition column has string values (e.g., with $ signs or commas like "$10,000"), sorting will use lexicographical order (not numeric), which might make it look like the sort didn't work. First convert it to a numeric type:

# Remove non-numeric characters and convert to float
df_test['tuition'] = df_test['tuition'].str.replace(r'[$,]', '', regex=True).astype(float)
# Now sort as before
sorted_students = df_test.sort_values( ['LastName', 'tuition'] )

内容的提问来源于stack exchange,提问作者Ryan1997123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:57:41