简体   繁体   English

如果同一 dataframe 中的另一列符合条件,如何从 pandas 中的列获取值?

[英]How to get values from a Column in pandas if another column in same dataframe matches a condition?

     Tax_Amount Rate    SGST    CGST    IGST    TDS Tax_Term

0   5697.0  9.0 NaN NaN NaN NaN CGST
1   954.0   9.0 NaN NaN NaN NaN TDS
2   1305.0  9.0 NaN NaN NaN NaN CGST
3   2724.0  9.0 NaN NaN NaN NaN SGST
4   18000.0 9.0 NaN NaN NaN NaN IGST

i Have a Dataframe as above, I want to have the Tax_Amount in all respective column if it matches the Colum Tax_Term.我有一个 Dataframe 如上所述,如果它与 Colum Tax_Term 匹配,我想在所有相应的列中都有 Tax_Amount。 Expected Output:-预计 Output:-

    Tax_Amount  Rate    SGST    CGST    IGST    TDS Tax_Term
0   5697.0  9.0 NaN 5697.0  NaN NaN CGST
1   954.0   9.0 NaN 954.0   NaN 954.0   TDS
2   1305.0  9.0 NaN 1305.0  NaN NaN CGST
3   2724.0  9.0 2724.0  NaN NaN NaN SGST
4   18000.0 9.0 NaN NaN 18000.0 NaN IGST

I tried doing the same with below code however i didn't get the desired result.我尝试用下面的代码做同样的事情,但是我没有得到想要的结果。

final_df['SGST'] = final_df.query('Tax_Term == SGST')['Tax_Amount']
final_df['CGST'] = final_df.query('Tax_Term == CGST')['Tax_Amount']
final_df['IGST'] = final_df.query('Tax_Term == IGST')['Tax_Amount']
final_df['TDS'] = final_df.query('Tax_Term == TDS')['Tax_Amount']

any help will be appretiated.任何帮助将不胜感激。 Thanks.谢谢。

You can use the apply method to achieve this.您可以使用 apply 方法来实现这一点。 The apply method allows you to apply a function to each row or column of a DataFrame. apply 方法允许您将 function 应用于 DataFrame 的每一行或每一列。

Here is an example of how you can use the apply method to accomplish your task:以下是如何使用 apply 方法完成任务的示例:

def fill_tax_amount(row):
    if row['Tax_Term'] == 'SGST':
        row['SGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'CGST':
        row['CGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'IGST':
        row['IGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'TDS':
        row['TDS'] = row['Tax_Amount']
    return row

final_df = final_df.apply(fill_tax_amount, axis=1)

This will apply the fill_tax_amount function to each row in the final_df DataFrame, and the function will fill the appropriate tax amount based on the value in the Tax_Term column.这会将 fill_tax_amount function 应用于 final_df DataFrame 中的每一行,function 将根据 Tax_Term 列中的值填充适当的税额。 The axis=1 argument specifies that the function should be applied to each row rather than each column. axis=1 参数指定 function 应应用于每一行而不是每一列。

Alternatively, you can use the pivot_table method to achieve the same result:或者,您可以使用 pivot_table 方法来获得相同的结果:

final_df = final_df.pivot_table(index=['Rate', 'TDS', 'Tax_Term'], 
                                columns='Tax_Term', 
                                values='Tax_Amount', 
                                aggfunc='first').reset_index()

暂无
暂无

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

相关问题 如何使用 pandas 根据同一数据帧中另一列的条件获取列值的连续平均值 - How to get consecutive averages of the column values based on the condition from another column in the same data frame using pandas 如何根据另一列的日期条件获取熊猫数据框中特定列的值? - How do I get the values of a particular column in a pandas dataframe based on a date condition on another column? 如果列名与另一个 DataFrame pandas 的行值匹配,则获取 DataFrame 的列值 - Get column values of a DataFrame if column name matches row value of another DataFrame pandas 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 根据条件从pandas数据框中获取一组列值 - Get a group of column values from pandas dataframe on condition 如何用条件 pandas python 替换另一列中的列的值 - how to replace the values of a column from another column with condition pandas python 在行匹配条件的Pandas DataFrame中获取第一列值 - Get first column value in Pandas DataFrame where row matches condition 用来自另一个数据框中的字符串匹配的平均值列向pandas数据框附加 - Append pandas dataframe with column of averaged values from string matches in another dataframe 当来自熊猫中另一个数据框的键匹配时更新数据框的列 - Update column of a dataframe when key matches from another dataframe in pandas 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM