python删除csv某一行_用Python一步从csv中删除特定的行和列

  • Post author:
  • Post category:python


步骤1,创建一个示例数据import pandas as pd

# Create sample CSV-file (100×100)

df = pd.DataFrame(np.arange(10000).reshape(100,100))

df.to_csv(‘test.csv’, index=False)

第二步import pandas as pd

import numpy as np

# Read first row to determine size of columns

size = pd.read_csv(‘test.csv’,nrows=0).shape[1]

#want to remove columns 25 to 29, in addition to columns 3 to 18 already specified,

# Ok so let’s create an array with the length of dataframe deleting the ranges

ranges = np.r_[3:19,25:30]

ar = np.delete(np.arange(size),ranges)

# Now let’s read the dataframe

# let us also skip rows 2 and 3

df = pd.read_csv(‘test.csv’, skiprows=[2,3], usecols=ar)

# And output

dt.to_csv(‘output.csv’, index=False)


1620