优化下列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 14:07:45 浏览: 151
Python 解决pandas.to-excel()函数覆盖原有Sheet页的问题 Python源码
& (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)
阅读全文