简体   繁体   English

在python中进行一些计算后如何添加新的新列

[英]How to add new a new column after doing some calculation in python

ID_REF      1007_s_at 1053_at 117_at 121_at 1255_g_at
GSM11111    0.08277 0.00874 0.00363 0.01877 0.00075
GSM95474    0.09503 0.00592 0.00352 0.01944 0.00055
GSM95475    0.08486 0.00678 0.00386 0.01973 0.00039
GSM95476    0.08105 0.00913 1       0.01801 0.00055
GSM95477    0.05918 0.00812 0.00428 0.01597 0.00033
GSM95478    0.07615 0.00777 0.00438 0.01799 0.00129
GSM95479    0.0976  0       0.00399 0.0216  0.00125
GSM95480    0.08499 0.00442 0.00298 0.01897 0.00015
GSM95481    0.08893 0.00734 0.00204 0.01706 0.00089
GSM99999    0.05981 0.01587 0.00365 0.01709 0.0006

This is my csv file data frame where i have certain values, i am looking to find the total count for them column-wise, with having some condition ( count all the values but ignore the 0's ) basically, what will happen is a new row will be added below GSM99999 named Final & below each column eg 1007_s_at and other columns saying 52 (where 52 is the total count of numeric values leaving the 0's in the excel file) 这是我的csv文件数据帧,在这里我有某些值,我希望逐列查找它们的总计数 ,并且要有一些条件( 计数所有值,但忽略0 ),基本上会发生新行下面GSM99999将加入一个名为决赛与每一列如1007_s_at等栏目下面说52(其中52是离开0的Excel文件中的数字值的总数)

i want to run this operation to the whole excel file regardless of how many column and row are there. 我想对整个Excel文件运行此操作,而不管有多少列和行。 I am currently using pandas and just started learning it. 我目前正在使用熊猫,并且刚刚开始学习它。

Here is the csv file image version : data csv file 这是csv文件映像版本: 数据csv文件

Here is the output i am looking for : output 这是我正在寻找的输出输出

Any help will be great for me, Thanks :) 任何帮助对我来说都是非常棒的,谢谢:)

import pandas as pd

df = pd.read_csv("<path to file>.csv").reset_index()

# update headers
df.columns = df.iloc[0]
df = df.iloc[1:].set_index("ID_REF")

df.loc["Final"] = ((df.notnull()) & (df != 0)).sum()

After reading he file from excel file in the dataframe df , you need: 从数据帧df中的excel文件中读取他的文件后,您需要:

df = df.set_index('ID_REF')
df = df.append(pd.DataFrame(dict(((df.notnull()) & (df != 0)).sum()), index=['Final']))

Output: 输出:

          1007_s_at 1053_at 117_at  121_at  1255_g_at
GSM11111    0.08277 0.00874 0.00363 0.01877 0.00075
GSM95474    0.09503 0.00592 0.00352 0.01944 0.00055
GSM95475    0.08486 0.00678 0.00386 0.01973 0.00039
GSM95476    0.08105 0.00913 1.00000 0.01801 0.00055
GSM95477    0.05918 0.00812 0.00428 0.01597 0.00033
GSM95478    0.07615 0.00777 0.00438 0.01799 0.00129
GSM95479    0.09760 0.00000 0.00399 0.02160 0.00125
GSM95480    0.08499 0.00442 0.00298 0.01897 0.00015
GSM95481    0.08893 0.00734 0.00204 0.01706 0.00089
GSM99999    0.05981 0.01587 0.00365 0.01709 0.00060
Final      10.00000 9.00000 10.00000 10.00000 10.00000

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

相关问题 对值进行一些计算后,从现有字典创建新字典 - Creating new dictionary from existing dictionary after doing some calculation to values 经过一些培训后如何使用countVectorizer测试新数据 - How to use countVectorizer to test new data after doing some training 如何将计算结果添加到数据框中的新列? - How do I add the results from a calculation to a new column in a dataframe? 如何通过包含一列某些值的平均值的计算在 DataFrame 中创建新列 - How to create a new column in a DataFrame from a calculation that includes the mean of some values of one column 在Python中,Pandas无法在datetime列之后添加新列 - In Python Pandas Can't add new column after datetime column Python,如何在excel中添加新列 - Python, how to add a new column in excel 如何在Python中标记关键字并添加到新列 - How to tag keywords and add to new column in Python 通过对现有列进行一些操作来创建新列 - Create a New Column by doing some operations on Existing Column python pandas 税收计算:根据给定的情景计算税收并将其添加到新列中 - python pandas tax calculation: calculate the tax according to the given senario and add it to a new column 如何在 python 的 dataframe 的列中添加一些计算 - How to add some calculation in columns of the dataframe in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM