Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - Iterating through a df with pandas to make new columns

I am currently trying for the first time to iterate through a set of columns (within a df) with the goal of creating a two new columns with:

1).one that totals up the number of 1's in the columns it iterated through 2).the second column if any of the iterated columns have a 1 in it, it puts a one in the new column and breaks. *** I solved this piece ***

ADDED MY DF for context:

   INDEX     ID    STATE Filed_Month  ...   MI   HD  Stroke  Diabeties_all
0      0  20190  Alabama     January  ...  2.0  2.0     2.0            3.0
1      1  20191  Alabama     January  ...  2.0  2.0     2.0            3.0
2      2  20192  Alabama     January  ...  2.0  2.0     2.0            1.0
3      3  20193  Alabama     January  ...  2.0  2.0     2.0            3.0
4      4  20194  Alabama     January  ...  2.0  2.0     2.0            3.0

[5 rows x 13 columns]

I also should mention when totaling up the 1's I am only interested in 7 out of the 13 columns.

I was able to get the second part of my question using the following:

def ifanyCM(row):
    if row["Asthma"] == 1:
        return 1
    if row["Asthma"] != 1:
        return 0
    if row["COPD_all"] == 1:
        return 1
    if row["COPD_all"] != 1:
        return 0
    if row["Skin_Cancer"] == 1:
        return 1
    if row["Skin_Cancer"] != 1:
        return 0
    if row["Other_Cancer"] == 1:
        return 1
    if row["Other_Cancer"] != 1:
        return 0
    if row["MI"] == 1:
        return 1
    if row["MI"] != 1:
        return 0
    if row["HD"] == 1:
        return 1
    if row["HD"] != 1:
        return 0
    if row["Stroke"] == 1:
        return 1
    if row["Stroke"] != 1:
        return 0
    if row["Diabeties_all"] == 1:
        return 1
    if row["Diabeties_all"] != 1:
        return 0
    
y2019_r1["CM"] = y2019_r1.apply(lambda row: ifanyCM(row), axis=1) 

I am just struggling to total up the ones out of the 7 columns of interest.

Cheers,

Rachel

question from:https://stackoverflow.com/questions/65833547/iterating-through-a-df-with-pandas-to-make-new-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's unclear what your dataframe looks like. But assuming it's a wide dataframe like this:

import pandas
import numpy

df = pandas.DataFrame({
    1: [1, 2, 1, 2, 7, 1, 1, 9],
    2: [2, 9, 2, 2, 2, 1, numpy.nan, numpy.nan]
}).fillna(0).astype(int).T.rename(
    index=lambda r: f"row{r}",
    columns=lambda r: f"col{r}",
)
      col0  col1  col2  col3  col4  col5  col6  col7
row1     1     2     1     2     7     1     1     9
row2     2     9     2     2     2     1     0     0

Then you don't need any loops or .apply at all (because apply is the same thing as a loop:

data = df.assign(
    CountOfOnes=lambda df: df.eq(1).sum(axis=1),
    HasAnyOne=lambda df: df.eq(1).any(axis=1)
)

Which is:

      col0  col1  col2  col3  col4  col5  col6  col7  CountOfOnes  HasAnyOne
row1     1     2     1     2     7     1     1     9            4       True
row2     2     9     2     2     2     1     0     0            1       True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...