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 multi column pandas.core.indexing . indexingerror: too many indexers error
- Pandas get column name
- TypeError: drop() got an unexpected keyword argument ‘columns’
- Reintex index of pandas
- Summary of three methods for pandas to convert dict into dataframe
- Three methods of converting dict into dataframe by pandas
- Python generates (x, y, z) 3D coordinate sequence
- Problem solving: Pandas: keyerror: [ ] not in index
- Converting string object into datetime type in pandas
- Interesting undefined columns selected from read.table and read.csv
- Httprunner v3. X generates HTML report in assure format
- Mybatis-plus automatically generates code error [Solved]
- IntelliJ idea automatically generates serialVersionUID
- [Python] pandas Library pd.to_ Parameter arrangement and example of Excel operation writing into excel file
- “Sh: dot: not found” when Doxygen generates documents
- Pandas sort according to a column_ values)
- When idea generates a spring boot project, error: read timed out appears
- In pandas, dataframe and np.array The mutual transformation between the two
- pandas parse_ Data exception, automatically skip