简体   繁体   中英

How to multiply a specific row in pandas dataframe by a condition

I have a column which of 10th marks but some specific rows are not scaled properly ie they are out of 10. I want to create a function that will help me to detect which are <=10 and then multiply to 100. I tried by creating a function but it failed. Following is the Column: data['10th']

0           0
1           0
2           0
3       10.00
4           0
        ...  
2163        0
2164        0
2165        0
2166    76.50
2167    64.60
Name: 10th, Length: 2168, dtype: object

I am not what do you mean by "multiply to 100" but you should be able to use apply with lambda similar to this:

df = pd.DataFrame({"a": [1, 3, 5, 23, 76, 43 ,12, 3 ,5]})
df['a'] = df['a'].apply(lambda x: x*100 if x < 10 else x)
print(df)

0   100
1   300
2   500
3   23
4   76
5   43
6   12
7   300
8   500

If I do not understand you correctly you could replace the action and condition in the lambda function to your purpose.

Looks like you need to change the data type first data["10th"] = pd.to_numeric(data["10th"]) I assume you want to multiply by 10 not 100 to scale it with the other out of 100 scores. you can try this np.where(data["10th"]<10, data["10th"]*10, data["10th"])

assigning it back to the dataframe using. data["10th"] = np.where(data["10th"]<10, data["10th"]*10, data["10th"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM