grouped = df.iloc[4:11].groupby(df.iloc[4:11]['第四列']).size()
时间: 2024-06-07 18:08:40 浏览: 18
这段代码的作用是对 DataFrame 的第 4 行到第 10 行进行切片(不包括第 11 行),然后按照第 4 列的值进行分组,统计每个组内的行数。具体来说,df.iloc[4:11] 是对 DataFrame 进行切片,只保留第 4 行到第 10 行的数据,而 df.iloc[4:11]['第四列'] 则是取出这些行中的第 4 列数据。然后,通过 groupby 方法对这些行按照第 4 列的值进行分组,使用 size 方法计算每个组内的行数。最终返回的是一个 Series 对象,其中每个元素代表一个组的行数。
相关问题
import pandas as pd import numpy as np import matplotlib.pyplot as plt table = pd.read_excel("order_quchong.xlsx") row = table.columns.tolist() for i in range(len(row)): if row[i] == 'item_price': oneindex1 = i elif row[i] == 'ord_qty': oneindex2 = i price = table.iloc[:,oneindex1].tolist() ordqty = table.iloc[:,oneindex2].tolist() Price = sorted(set(price)) OrdNumeber = [] for i in range(len(Price)): sum = 0 for j in range(len(ordqty)): if price[j] == Price[i]: sum += ordqty[j] OrdNumeber.append(int(sum)) x = np.arange(1, max(Price) + 0.01, 0.001) y = np.interp(x, Price, OrdNumeber) plt.plot(x, y) plt.show()优化代码
可以使用pandas的groupby方法和agg方法对表格数据进行聚合操作,用于计算每个item_price对应的ord_qty总和,而无需通过for循环遍历每个item_price。代码如下:
```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
table = pd.read_excel("order_quchong.xlsx")
# 使用groupby方法对item_price分组,并使用agg方法计算每个分组的ord_qty总和
grouped_table = table.groupby('item_price')['ord_qty'].agg('sum')
# 获取item_price和ord_qty数据
Price = grouped_table.index.tolist()
OrdNumeber = grouped_table.tolist()
# 使用numpy的arange方法生成数据范围
x = np.arange(1, max(Price), 0.01)
# 使用numpy的interp方法对item_price和ord_qty进行插值
y = np.interp(x, Price, OrdNumeber)
# 绘制图像
plt.plot(x, y)
plt.show()
```
忽略该脚本警告 import pandas as pd import glob def com(): file_paths = glob.glob('E:/py卓望/数据分析/top150_20230321/*.txt') data = pd.DataFrame() for i in file_paths: df = pd.read_csv(i, sep=',', header=None, skiprows=[0]) data = pd.concat([data, df]) data.drop(df.columns[0], axis=1, inplace=True) df.sort_values(by=1, ascending=False, inplace=True) data.iloc[:, 0] = data.iloc[:, 0].str.lower() data.to_csv('E:/py卓望/数据分析/all/all_file.txt', sep=',', index=False,header=False) all = pd.read_csv('E:/py卓望/数据分析/all/all_file.txt', header=None, delimiter=',') all[0] = all[0].str.split('.') all[0] = all[0].apply( lambda x: '.'.join(x[-3:]) if '.'.join(x[-2:]) in ['gov.cn', 'com.cn', 'org.cn', 'net.cn'] else '.'.join(x[-2:])) new_col = all[0] result = pd.concat([new_col,all.iloc[:,1:]],axis=1) result.to_csv('E:/py卓望/数据分析/all/二级域名.txt', sep=',',index=False,header=False) summation = pd.read_csv('E:/py卓望/数据分析/all/二级域名.txt', header=None, delimiter=',') grouped = summation.groupby(0)[1].sum().reset_index() grouped = grouped.sort_values(by=1, ascending=False).reset_index(drop=True) grouped[1] = grouped[1].fillna(summation[1]) grouped.to_csv('E:/py卓望/数据分析/all/处理后求和域名.txt', sep=',', index=False, header=False) top_10000 = pd.read_csv('E:/py卓望/数据分析/all/处理后求和域名.txt', header=None, delimiter=',') alls = top_10000.nlargest(10000, 1) alls.drop(columns=[1], inplace=True) alls.to_csv('E:/py卓望/数据分析/all/data.txt', sep=',',index=False, header=False) final = top_10000.iloc[10000:] final.drop(columns=[1], inplace=True) final.to_csv('E:/py卓望/数据分析/all/final_data.txt', sep=',',index=False, header=False) print(final.to_csv) warnings.filterwarnings("ignore") def main(): com() if __name__ == "__main__": print("开始清洗域名文件") main() print("数据清洗完毕")
要忽略该脚本中的警告,可以在脚本开头添加以下两行代码:
```python
import warnings
warnings.filterwarnings("ignore")
```
这会忽略脚本中的所有警告。将这两行代码添加到脚本的开头即可:
```python
import pandas as pd
import glob
import warnings
def com():
warnings.filterwarnings("ignore")
file_paths = glob.glob('E:/py卓望/数据分析/top150_20230321/*.txt')
data = pd.DataFrame()
for i in file_paths:
df = pd.read_csv(i, sep=',', header=None, skiprows=[0])
data = pd.concat([data, df])
data.drop(df.columns[0], axis=1, inplace=True)
df.sort_values(by=1, ascending=False, inplace=True)
data.iloc[:, 0] = data.iloc[:, 0].str.lower()
data.to_csv('E:/py卓望/数据分析/all/all_file.txt', sep=',', index=False,header=False)
all = pd.read_csv('E:/py卓望/数据分析/all/all_file.txt', header=None, delimiter=',')
all[0] = all[0].str.split('.')
all[0] = all[0].apply(
lambda x: '.'.join(x[-3:]) if '.'.join(x[-2:]) in ['gov.cn', 'com.cn', 'org.cn', 'net.cn'] else '.'.join(x[-2:]))
new_col = all[0]
result = pd.concat([new_col,all.iloc[:,1:]],axis=1)
result.to_csv('E:/py卓望/数据分析/all/二级域名.txt', sep=',',index=False,header=False)
summation = pd.read_csv('E:/py卓望/数据分析/all/二级域名.txt', header=None, delimiter=',')
grouped = summation.groupby(0)[1].sum().reset_index()
grouped = grouped.sort_values(by=1, ascending=False).reset_index(drop=True)
grouped[1] = grouped[1].fillna(summation[1])
grouped.to_csv('E:/py卓望/数据分析/all/处理后求和域名.txt', sep=',', index=False, header=False)
top_10000 = pd.read_csv('E:/py卓望/数据分析/all/处理后求和域名.txt', header=None, delimiter=',')
alls = top_10000.nlargest(10000, 1)
alls.drop(columns=[1], inplace=True)
alls.to_csv('E:/py卓望/数据分析/all/data.txt', sep=',',index=False, header=False)
final = top_10000.iloc[10000:]
final.drop(columns=[1], inplace=True)
final.to_csv('E:/py卓望/数据分析/all/final_data.txt', sep=',',index=False, header=False)
print(final.to_csv)
def main():
print("开始清洗域名文件")
com()
print("数据清洗完毕")
if __name__ == "__main__":
main()
```
这样就可以在运行脚本时忽略所有警告了。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)