Home > other >  Python pandas dataframe cycle judgment assignment new column
Python pandas dataframe cycle judgment assignment new column

Time:01-08


Id
1
2
1
2
3
1
2
3
4
One column. CSV form, I want to use loop statement generated new table is as follows:
Id gr
1 1
2 1
1
22 2
3 2
1 3
2, 3,
3 3
4 3
Feeling a bit like playing group, have a great god to help overcome?
I write my own code is as follows:
The import pandas as pd

Data=https://bbs.csdn.net/topics/pd.read_csv (' C:/Users/lenovo/Desktop/01. CSV ', engine='python', encoding='unicode_escape')
A=1
Num=0 # counter,, that is, the value of the column to fill in b
For I in data [' id '] : # assumes that the first column array is called aarray
If a==1:
Num +=1
Data. Loc [I, 'gr]=# num is assuming that the second column array is called barray
The else:
Data. Loc [I, 'gr]=data. The loc [I,' gr]
This code doesn't realize!
  • Related