问题
I have a data frame like following below.
coutry flag
0 China red
1 Russia green
2 China yellow
3 Britain yellow
4 Russia green
......................
In df['country'], you can see many different country names. I want to set the first appear country as 1, the second as 2. The flag is the same logic.So you can see the result is:
coutry flag
0 1 1
1 2 2
2 1 3
3 3 3
4 2 2
But I don't know how to achieve this logic in python. Thank you. Moreover when I get the result data frame, I want to have an function to back data frame to the original.
回答1:
You can use factorize and add 1
:
df['coutry'] = pd.factorize(df.coutry)[0] + 1
df['flag'] = pd.factorize(df.flag)[0] + 1
print (df)
coutry flag
0 1 1
1 2 2
2 1 3
3 3 3
4 2 2
Then you can convert columns to categories
by Categorical if need save memory:
df['coutry'] = pd.Categorical(pd.factorize(df.coutry)[0] + 1)
df['flag'] = pd.Categorical(pd.factorize(df.flag)[0] + 1)
print (df)
coutry flag
0 1 1
1 2 2
2 1 3
3 3 3
4 2 2
print (df.dtypes)
coutry category
flag category
dtype: object
#1000 times larger df
df = pd.concat([df]*1000).reset_index(drop=True)
df['coutry'] = pd.Categorical(pd.factorize(df.coutry)[0] + 1)
df['flag'] = pd.factorize(df.flag)[0] + 1
print (df)
coutry flag
0 1 1
1 2 2
2 1 3
3 3 3
4 2 2
5 1 1
6 2 2
...
...
print (df['coutry'].nbytes)
5024
print (df['flag'].nbytes)
20000
If need convert back, you can map values by dictionaries:
b = [list(x) for x in pd.factorize(df.coutry.drop_duplicates())]
d1 = dict(zip(b[0], b[1]))
print (d1)
{0: 'China', 1: 'Russia', 2: 'Britain'}
b = [list(x) for x in pd.factorize(df.flag.drop_duplicates())]
d2 = dict(zip(b[0], b[1]))
print (d2)
{0: 'red', 1: 'green', 2: 'yellow'}
df['coutry'] = pd.Categorical(pd.factorize(df.coutry)[0])
df['flag'] = pd.Categorical(pd.factorize(df.flag)[0])
print (df)
coutry flag
0 0 0
1 1 1
2 0 2
3 2 2
4 1 1
df['coutry'] = df.coutry.map(d1)
df['flag'] = df.flag.map(d2)
print (df)
coutry flag
0 China red
1 Russia green
2 China yellow
3 Britain yellow
4 Russia green
来源:https://stackoverflow.com/questions/38966912/how-to-deal-with-this-logic-in-pandas