A very convenient way to use pandas is through indexes such as LOC, iloc and IX. here is a record:
df.loc [condition, new column] = assign initial value
If the new column name is an existing column name, it will be changed on the original data column
import pandas as pd
import numpy as np
data = pd.DataFrame ( np.random.randint (0,100,40).reshape(10,4),columns=list(‘abcd’))
print(data)
data.loc [data. D & gt; = 50, ‘greater than 50′] =’Yes’
Print (data)
By using LOC to index, judge in the index, and then assign value to the new column according to the result of judgment. This is a very convenient and basic operation. Of course, I don’t remember it clearly recently, so I’ll record it here.
Read More:
- Methods of modifying index and columns names by dataframe in pandas
- Pandas get column name
- Python common error: if using all scalar values, you must pass an index (four solutions)
- Summary of three methods for pandas to convert dict into dataframe
- Converting string object into datetime type in pandas
- Three methods of converting dict into dataframe by pandas
- Reintex index of pandas
- [Python] pandas Library pd.to_ Parameter arrangement and example of Excel operation writing into excel file
- The sparse matrix of R language is too large to be used as.matrix
- Python random selects elements randomly from a collection
- Tensorflow in function tf.Print Method of outputting intermediate value
- The solution to the error of importing CSV from Python to Python
- 26 English letters in upper and lower case and 0-9 to generate 8-bit random password
- Common attributes and methods of list and map in Dar
- Pre initialization of list content and length in Python
- TypeError: drop() got an unexpected keyword argument ‘columns’
- Pg_dump Error: pg_dump: No matching tables were found,pg_dump: schema with OID 1515227 does not exi
- Origin — draw the curve with error bar
- fix yahoo finance
- On set in pandas_ Index and reset_ Usage of index