如何保存csv-s中的特定行范围



所以我的代码所做的是水平合并相同大小(相同列和行数(的csv-s。csv-s相当大(每行1M(,所以我只想保存0-20行和70-80行的文件,它们之间有一个空行。有可能吗?

理想情况下,代码甚至不会完全合并csv-s,只合并所有所需的范围

import pandas as pd
import os

def get_df():
df=pd.DataFrame()
os.chdir("C:/Users/DD/Desktop/TM/Simulations/1-2")
for file in os.listdir():
if file.endswith('.csv'):
aux=pd.read_csv(file, error_bad_lines=False)
df = pd.concat([df, aux], axis=1, join='outer')

return df
df=get_df()
df.to_csv(f"file_name.csv")

将其从注释中删除。使用第一条评论中建议的iloc[np.r_[]]如预期的那样工作:

import numpy as np
import pandas as pd
rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(0, 100, size=(100, 4)), columns=list('ABCD'))
df = df.iloc[np.r_[0:20,70:80]]
df2 = pd.DataFrame(rng.integers(0, 100, size=(100, 4)), columns=list('EFGH'))
display(pd.concat([df, df2.iloc[np.r_[0:20,70:80]]], axis=1, join='outer'))

+-----+----+----+----+----+----+----+----+----+
| idx | A  | B  | C  | D  | E  | F  | G  | H  |
+-----+----+----+----+----+----+----+----+----+
|   0 | 47 | 31 | 52 | 91 | 65 |  3 | 77 | 58 |
|   1 | 10 | 32 | 21 | 36 | 28 | 72 | 31 | 93 |
|   2 | 29 | 96 |  2 | 69 | 72 | 47 | 59 | 78 |
|   3 | 69 | 31 | 76 | 42 | 96 | 26 | 59 | 36 |
|   4 | 50 | 49 | 66 |  2 |  4 | 38 | 97 | 11 |
|   5 | 72 | 29 | 74 | 90 | 79 | 84 | 39 | 10 |
|   6 |  6 |  5 | 43 | 31 | 64 | 37 | 68 |  4 |
|   7 | 93 |  8 | 52 | 19 | 15 | 68 | 58 | 20 |
|   8 | 91 | 23 | 99 | 31 | 95 | 36 | 51 | 65 |
|   9 | 68 | 68 | 65 | 21 | 23 | 22 |  6 | 30 |
|  10 | 29 | 35 |  3 | 62 | 44 | 33 | 28 | 17 |
|  11 | 44 | 89 | 13 | 83 | 35 | 28 | 93 | 95 |
|  12 | 48 | 25 |  7 | 67 | 33 | 21 | 33 | 30 |
|  13 | 28 | 93 | 92 | 82 | 83 | 48 |  4 | 51 |
|  14 | 50 | 50 | 15 | 25 |  2 | 57 | 15 | 73 |
|  15 |  4 | 77 | 78 | 68 | 35 |  0 | 49 | 16 |
|  16 | 57 | 53 | 87 |  5 | 50 | 31 | 55 | 79 |
|  17 | 46 | 21 | 36 | 15 | 85 | 43 | 64 | 20 |
|  18 | 43 | 62 | 23 | 35 | 85 | 71 | 37 | 52 |
|  19 | 27 | 36 | 73 | 60 | 16 | 22 | 37 | 42 |
|  70 | 46 | 86 | 93 | 66 | 13 | 57 |  4 | 68 |
|  71 | 68 | 30 | 42 | 60 | 99 | 31 | 50 | 42 |
|  72 | 31 | 91 | 41 | 18 | 75 | 88 | 83 | 15 |
|  73 | 25 |  5 | 53 | 31 | 16 | 33 | 64 | 74 |
|  74 | 35 | 56 | 58 | 91 | 34 | 75 | 29 | 38 |
|  75 | 66 | 10 | 36 | 37 | 33 | 36 | 10 |  2 |
|  76 | 99 | 43 | 22 | 38 | 52 | 54 |  0 | 77 |
|  77 | 76 | 58 | 89 | 25 | 79 | 16 | 50 | 23 |
|  78 | 15 | 59 | 20 |  4 | 83 | 69 | 60 | 36 |
|  79 | 81 | 12 |  0 | 92 | 43 | 22 | 16 |  4 |
+-----+----+----+----+----+----+----+----+----+

相关内容

  • 没有找到相关文章

最新更新