简体   繁体   English

如何使用计算值在 pandas 中创建新列并为每一行分配特定值?

[英]How do I create a new column in pandas using calculated values and assign specific values to each row?

I have a dataframe df like我有一个 dataframe df 喜欢

      Cards                   Height                    State Hash
0    1615427359331  ...         1945  3NK38AjJUMDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...
0    1615427359331  ...         1952  3NK38AjJUMvcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...
0    1615427359331  ...         1958  3NK38AjJUMDhEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...
0    1615427359331  ...         1952  3NK38AjJUMDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...
0    1615427359331  ...         1957  3NK38AjJUMhDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT..

I have calculated the value of the most occuring State hash in the dataframe and the median of height我已经计算了 dataframe 中出现次数最多的 State hash 的值和高度的中位数

Using these two calculated values, how can I create a New column ON/OFF.使用这两个计算值,我如何创建一个新列 ON/OFF。 and set the value as 1 or 0. Value 1 would be set if those two calculated values are matching else the value would be set to 0. How do I do that using Pandas dataframe?并将值设置为 1 或 0。如果这两个计算值匹配,则将设置值 1,否则将值设置为 0。如何使用 Pandas dataframe 执行此操作? Please help.请帮忙。 Thanks!谢谢!

**EDIT: ** This is what ive tried so far **编辑:**这是我迄今为止尝试过的

if [(dataframe['Block Height'] == median_highest_blocklen_recievd) & (dataframe['State Hash'] == most_commonrec_stat_hash)]:
    dataframe['online/offline'] = 1
if [(dataframe['Block Height'] != median_highest_blocklen_recievd) | (dataframe['State Hash'] != most_commonrec_stat_hash)]:
    dataframe['online/offline'] = -1

and This gave me the dataframe as这给了我 dataframe 作为

      Cards                   Height                    State Hash   Online/Offline
                                                      
0    1615427359331  ...         1945  3NK38AjJUMDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...    -1
0    1615427359331  ...         1952  3NK38AjJUMvcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...    -1
0    1615427359331  ...         1958  3NK38AjJUMDhEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...    -1
0    1615427359331  ...         1952  3NK38AjJUMDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT...    -1
0    1615427359331  ...         1957  3NK38AjJUMhDcEkPVaTWKsMcccUWXywgpMRseaNNPz6gyhT..    -1

I'm not too sure what your use case is, but you definitely want to use numpy's where我不太确定你的用例是什么,但你肯定想在哪里使用 numpy

For example:例如:

df['ON/OFF'] = np.where((df['Height']==median_height) & (df['State Hash']==mode_hash),1,0)

One approach is to do it with apply:一种方法是使用 apply:

df['New'] = df.apply(lambda x: 1 if x['Height'] == median_height and x['State Hash'] == calculated_hash else 0, axis = 1)

暂无
暂无

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

相关问题 如何使用熊猫创建一个新列的最大值(对应于特定名称)? - How do I create a new column of max values of a column(corresponding to specific name) using pandas? 创建一个新列并按 Python Pandas 中的每个组将值分配给第一行 - Create a new column and assign values to the first row by each group in Python Pandas 使用 python pandas 将行值转换为列以将日期字段分配给每个新列 - Convert row values to columns to assign date field to each new column using python pandas 如何使用 Pandas 将值分配给新类别? - How do I assign values to a new category using Pandas? 如何创建一个包含特定值的新pandas DataFrame? - How do I create a new pandas DataFrame containing specific values? Pandas DataFrame:添加具有基于前一行计算值的新列 - Pandas DataFrame: Add new column with calculated values based on previous row 如何使用Pandas在CSV文件中创建新列,并根据这些列中的值添加数据 - How do I create a new column in a csv file using Pandas, and add data depending on the values in those columns 如何为 pandas 数据库中的特定行和列分配值? - How do I assign a value to a specific row and column in a pandas database? 如何通过特定列分配新的值列? - how to assign new column of values by a specific column? Pandas:在数据框中创建一个新列,其中的值是从现有列 i 计算出来的。 计算最大值 - Pandas: Create a new column in a data frame with values calculated from an already existing column, i. calculate maximum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM