如何读取和处理文件的一部分并将其余部分写入另一个文件?

问题描述 投票:1回答:2

我需要用Panda阅读这个csv file,对它执行一些处理,然后将剩余的10%的数据写入另一张表。

鉴于此解决方案(https://stackoverflow.com/a/55763598/3373710),我想在取出10%行后对store_data的其余部分执行一个处理,但是,elif条件打印原始文件的相同行,如何修复我的条件以跳过10%行?

store_data = pd.read_csv("heart_disease.csv")

with open("out1.csv","w") as outfile:
    outcsv = csv.writer(outfile)
    for i, row in store_data.iterrows():
        if not i % 10: #write 10% of the file to another file
            outcsv.writerow(row)
        elif i % 10:  #I need to do some process on the rest of the file
            store_data = store_data.applymap(str)
python pandas test-data
2个回答
1
投票

简单地将数据框分成两部分,将10%保存到文件(dataframe.to_csv(..))并将计算应用到第二个df中的90%更简单,更简洁。

您可以通过计算一个新列来告诉您行是否为test,并将此数据帧分成两个新列值:

数据文件创建:

fn = "heart_disease.csv"
with open(fn,"w") as f:
    # doubled the data provided
    f.write("""Age,AL,SEX,DIAB,SMOK,CHOL,LAD,RCA,LM
65,0,M,n,y,220,80,75,20\n45,0.2,F,n,n,300,90,35,35\n66,-1,F,y,y,200,90,80,20
70,0.2,F,n,y,220,40,85,15\n80,1.1,M,y,y,200,90,90,25\n55,0,M,y,y,240,95,45,25
90,-1,M,n,y,350,35,75,20\n88,1,F,y,y,200,40,85,20\n50,1.1,M,n,n,220,55,30,30
95,-1,M,n,y,230,75,85,15\n30,1.1,F,n,y,235,75,20,30
65,0,M,n,y,220,80,75,20\n45,0.2,F,n,n,300,90,35,35\n66,-1,F,y,y,200,90,80,20
70,0.2,F,n,y,220,40,85,15\n80,1.1,M,y,y,200,90,90,25\n55,0,M,y,y,240,95,45,25
90,-1,M,n,y,350,35,75,20\n88,1,F,y,y,200,40,85,20\n50,1.1,M,n,n,220,55,30,30
95,-1,M,n,y,230,75,85,15\n30,1.1,F,n,y,235,75,20,30
""") 

程序:

import pandas as pd

fn = "heart_disease.csv"
store_data = pd.read_csv(fn)
print(store_data)

import random
import numpy as np

percentage = 0.1 
store_data["test"] = np.random.rand(len(store_data)) 

test_data = store_data[store_data.test <= percentage]
other_data = store_data[store_data.test > percentage]

print(test_data)
print(other_data)

输出:

# original data 
    Age   AL SEX DIAB SMOK  CHOL  LAD  RCA  LM
0    65  0.0   M    n    y   220   80   75  20
1    45  0.2   F    n    n   300   90   35  35
2    66 -1.0   F    y    y   200   90   80  20
3    70  0.2   F    n    y   220   40   85  15
4    80  1.1   M    y    y   200   90   90  25
5    55  0.0   M    y    y   240   95   45  25
6    90 -1.0   M    n    y   350   35   75  20
7    88  1.0   F    y    y   200   40   85  20
8    50  1.1   M    n    n   220   55   30  30
9    95 -1.0   M    n    y   230   75   85  15
10   30  1.1   F    n    y   235   75   20  30
11   65  0.0   M    n    y   220   80   75  20
12   45  0.2   F    n    n   300   90   35  35
13   66 -1.0   F    y    y   200   90   80  20
14   70  0.2   F    n    y   220   40   85  15
15   80  1.1   M    y    y   200   90   90  25
16   55  0.0   M    y    y   240   95   45  25
17   90 -1.0   M    n    y   350   35   75  20
18   88  1.0   F    y    y   200   40   85  20
19   50  1.1   M    n    n   220   55   30  30
20   95 -1.0   M    n    y   230   75   85  15
21   30  1.1   F    n    y   235   75   20  30

# data  with test <= 0.1
    Age   AL SEX DIAB SMOK  CHOL  LAD  RCA  LM      test
3    70  0.2   F    n    y   220   40   85  15  0.093135
10   30  1.1   F    n    y   235   75   20  30  0.021302

# data with test > 0.1
    Age   AL SEX DIAB SMOK  CHOL  LAD  RCA  LM      test
0    65  0.0   M    n    y   220   80   75  20  0.449546
1    45  0.2   F    n    n   300   90   35  35  0.953321
2    66 -1.0   F    y    y   200   90   80  20  0.928233
4    80  1.1   M    y    y   200   90   90  25  0.672880
5    55  0.0   M    y    y   240   95   45  25  0.136537
6    90 -1.0   M    n    y   350   35   75  20  0.439261
7    88  1.0   F    y    y   200   40   85  20  0.935340
8    50  1.1   M    n    n   220   55   30  30  0.737416
9    95 -1.0   M    n    y   230   75   85  15  0.461699
11   65  0.0   M    n    y   220   80   75  20  0.548624
12   45  0.2   F    n    n   300   90   35  35  0.679861
13   66 -1.0   F    y    y   200   90   80  20  0.195141
14   70  0.2   F    n    y   220   40   85  15  0.997854
15   80  1.1   M    y    y   200   90   90  25  0.871436
16   55  0.0   M    y    y   240   95   45  25  0.907141
17   90 -1.0   M    n    y   350   35   75  20  0.295690
18   88  1.0   F    y    y   200   40   85  20  0.970249
19   50  1.1   M    n    n   220   55   30  30  0.566218
20   95 -1.0   M    n    y   230   75   85  15  0.545188
21   30  1.1   F    n    y   235   75   20  30  0.217490 

它是随机的,您可能会获得10%的数据 - 或者您可以获得更少/超过10% - 数据越大,您就越接近10%。

您可以使用“派生”数据帧使用df.to_csv将数据存储到测试和其他数据中。

对于一个纯粹的熊猫解决方案How do I create test and train samples from one dataframe with pandas?是你的副本,但你似乎单独处理csv所以不确定它是否适用。


1
投票

这是一个纯粹的熊猫解决方案:

import pandas as pd
df = pd.read_csv("heart_disease.csv")
#select only 10% of the rows, subtract 1 because index starts with zero
df_slice = df.loc[:round(len(df) * 10 /100) - 1, :]
#write the sliced df to csv
df_slice.to_csv("sliced.csv", index=None)
#to work with the rest of the data, just drop the rows at index where the df_slice rows exist
l = df_slice.index.tolist()
df.drop(df.index[l], inplace=True) #90% of data
#now the df has the rest 90% and you can do whatever you want with it
© www.soinside.com 2019 - 2024. All rights reserved.