﻿ Python之DataFrame按照规则批量修改某列的数据 - 军军小站|张军博客

# Python之DataFrame按照规则批量修改某列的数据

在使用Python进行数据分析时，我们经常会看一个数据的分布，然后对数据进行处理。比如说有一个场景：

```            ```
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
avg_days_befor = (data['avg_days_befor'].value_counts()).reset_index()
sns.barplot(x = avg_days_befor.index, y = avg_days_befor['avg_days_befor'])
plt.show() # 图3
```
```

```            ```
```
```

```            ```
#观察下提前预定天数的分布
avg_days_befor.describe()
```
```

index avg_days_befor
count 259.000000 259.000000
mean 139.382239 485.555985
std 97.080785 1240.626948
min 0.000000 1.000000
25% 64.500000 2.000000
50% 129.000000 17.000000
75% 194.500000 180.000000
max 633.000000 10273.000000

```            ```
#对于提前预定天数重写
def fun_avg_days_befor(x):
if x <= 7:
return 1
elif x > 7 and x <= 14:
return 2
elif x > 14 and x <= 21:
return 3
elif x > 21 and x <= 28:
return 4
elif x > 28 and x <= 35:
return 5
elif x > 35 and x <= 42:
return 6
elif x > 42 and x <= 49:
return 7
elif x > 49 and x <= 56:
return 8
elif x > 56 and x <= 63:
return 9
else:
return 10

#将数据生效在原dataframe上
data['avg_days_befor'] = data['avg_days_befor'].apply(lambda x:fun_avg_days_befor(x))

avg_days_befor = (data['avg_days_befor'].value_counts()).reset_index()
```
```

```            ```
sns.barplot(x = avg_days_befor.index, y = avg_days_befor['avg_days_befor'])
plt.show() # 图3
```
```

```            ```
#对于提前预定天数重写
def fun_avg_days_befor(x):
if x <= 7:
return 1
elif x > 7 and x <= 14:
return 2
elif x > 14 and x <= 21:
return 3
elif x > 21 and x <= 28:
return 4
elif x > 28 and x <= 35:
return 5
elif x > 35 and x <= 42:
return 6
elif x > 42 and x <= 49:
return 7
elif x > 49 and x <= 56:
return 8
elif x > 56 and x <= 63:
return 9
else:
return 10

data['avg_days_befor'].apply(lambda x:fun_avg_days_befor(x))
```
```

QQ号联系： 360901061

【本文对您有帮助就好】