优化下列python代码:import pandas as pd import numpy as np pd.set_option('display.max_columns',None) df1=pd.read_csv('sample.csv',sep=',') print(df1[:10]) print(df1.columns) Sichuan_chongqing=df1[(df1['departure_province']=='四川')& (df1['destination_province']=='重庆')] print(Sichuan_chongqing[:10]) Sichuan_chongqing.to_csv('四川到重庆.ccv') Sichuan_chongqing.to_csv('Sichuan_chongqing.csv')
时间: 2023-05-12 22:07:45 浏览: 144
详解pandas删除缺失数据(pd.dropna()方法)
5星 · 资源好评率100%
& (df1['arrival_province']=='重庆')] Sichuan_chongqing.to_csv('Sichuan_chongqing.csv',index=False)
优化后的代码如下:
import pandas as pd
pd.set_option('display.max_columns', None)
df1 = pd.read_csv('sample.csv', sep=',')
print(df1.head(10))
print(df1.columns)
Sichuan_chongqing = df1.loc[(df1['departure_province'] == '四川') & (df1['arrival_province'] == '重庆')]
Sichuan_chongqing.to_csv('Sichuan_chongqing.csv', index=False)
阅读全文