当python中的特定条件为真时,更改csv的值

2024-04-28 22:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把前缀为“0.0”的值乘以10。在

下面是我的csv文件的例子。在

1987-01-14, 0.63, 0.0657, 0.0652, 0.0677
1987-01-15, 0.0639, 0.0662, 0.0652, 0.0676
1987-01-16, 0.0642, 0.0662, 0.0653, 0.0678
1987-01-19, 0.0644, 0.0663, 0.0653, 0.0677
1987-01-20, 0.065, 0.0664, 0.0653, 0.0676
1987-01-21, 0.0652, 0.0666, 0.0658, 0.0679
1987-01-22, 0.0651, 0.0672, 0.0662, 0.0684
1987-01-23, 0.0652, 0.0672, 0.0661, 0.0687
1987-01-26, 0.0654, 0.0674, 0.0663, 0.0688
1987-01-27, 0.0661, 0.0681, 0.0656, 0.0681

import csv
import math,pandas
s = open('input1.csv')
checkIt = csv.reader(s)
for c in checkIt:
       #print(line)
    #values = c.split(',')
    for value in c:
        if "0.0" in value:
            int_v =10* float(value)
            print (int_v)

提前谢谢


Tags: 文件csvinimportpandasforvaluemath
3条回答
import csv
with open('input1.csv') as csvfile:
    spamreader = csv.reader(csvfile)
    lines = list(spamreader)
    for row in lines:
        for i, value in enumerate(row):
            if value.startswith("0.0"):
                row[i] = "{0:.2f}".format(float(value)*10)
with open('eggs.csv', 'w') as csvfile:
    spamwriter = csv.writer(csvfile)
    for row in lines:
        spamwriter.writerow(row)

out:
1987-01-13,0,0.66,0.66,0.65,0.68,0.68
1987-01-14,0.63,0.66,0.66,0.65,0.68,0.68
1987-01-15,0.64,0.66,0.66,0.65,0.68,0.68
1987-01-16,0.64,0.66,0.66,0.65,0.68,0.68
1987-01-19,0.64,0.66,0.66,0.66,0.68,0.68
1987-01-20,0.65,0.67,0.67,0.66,0.69,0.69
1987-01-21,0.65,0.67,0.67,0.66,0.69,0.69
1987-01-22,0.65,0.67,0.67,0.66,0.68,0.68
1987-01-23,0.65,0.67,0.67,0.66,0.68,0.68

一种不转换为floats的可能解决方案-检查if 0.0,并删除0.之后的第一个0。非常重要的是在read_cvcsv中设置dtype=str参数,以便将所有列转换为str

import pandas as pd
import numpy as np

temp=u"""1987-01-13 0 0.0657 0.0657 0.0652 0.0678 0.0678
1987-01-14 0.63 0.0662 0.0662 0.0653 0.0677 0.0677
1987-01-15 0.0639 0.0662 0.0662 0.0653 0.0676 0.0676
1987-01-16 0.0642 0.0663 0.0663 0.0653 0.0679 0.0679
1987-01-19 0.0644 0.0664 0.0664 0.0658 0.0684 0.0684
1987-01-20 0.065 0.0666 0.0666 0.0662 0.0687 0.0687
1987-01-21 0.0652 0.0672 0.0672 0.0661 0.0688 0.0688
1987-01-22 0.0651 0.0672 0.0672 0.0663 0.0681 0.0681
1987-01-23 0.0652 0.0674 0.0674 0.0656 0.0681 0.0681"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep="\s+", header=None, dtype=str, index_col=[0])

print (df)
                 1       2       3       4       5       6
0                                                         
1987-01-13       0  0.0657  0.0657  0.0652  0.0678  0.0678
1987-01-14    0.63  0.0662  0.0662  0.0653  0.0677  0.0677
1987-01-15  0.0639  0.0662  0.0662  0.0653  0.0676  0.0676
1987-01-16  0.0642  0.0663  0.0663  0.0653  0.0679  0.0679
1987-01-19  0.0644  0.0664  0.0664  0.0658  0.0684  0.0684
1987-01-20   0.065  0.0666  0.0666  0.0662  0.0687  0.0687
1987-01-21  0.0652  0.0672  0.0672  0.0661  0.0688  0.0688
1987-01-22  0.0651  0.0672  0.0672  0.0663  0.0681  0.0681
1987-01-23  0.0652  0.0674  0.0674  0.0656  0.0681  0.0681

^{pr2}$

floats-乘以10的解,如果某个值小于0.1

import pandas as pd
import numpy as np

temp=u"""1987-01-13 0 0.0657 0.0657 0.0652 0.0678 0.0678
1987-01-14 0.63 0.0662 0.0662 0.0653 0.0677 0.0677
1987-01-15 0.0639 0.0662 0.0662 0.0653 0.0676 0.0676
1987-01-16 0.0642 0.0663 0.0663 0.0653 0.0679 0.0679
1987-01-19 0.0644 0.0664 0.0664 0.0658 0.0684 0.0684
1987-01-20 0.065 0.0666 0.0666 0.0662 0.0687 0.0687
1987-01-21 0.0652 0.0672 0.0672 0.0661 0.0688 0.0688
1987-01-22 0.0651 0.0672 0.0672 0.0663 0.0681 0.0681
1987-01-23 0.0652 0.0674 0.0674 0.0656 0.0681 0.0681"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep="\s+", header=None, index_col=[0])

print (df)
                 1       2       3       4       5       6
0                                                         
1987-01-13  0.0000  0.0657  0.0657  0.0652  0.0678  0.0678
1987-01-14  0.6300  0.0662  0.0662  0.0653  0.0677  0.0677
1987-01-15  0.0639  0.0662  0.0662  0.0653  0.0676  0.0676
1987-01-16  0.0642  0.0663  0.0663  0.0653  0.0679  0.0679
1987-01-19  0.0644  0.0664  0.0664  0.0658  0.0684  0.0684
1987-01-20  0.0650  0.0666  0.0666  0.0662  0.0687  0.0687
1987-01-21  0.0652  0.0672  0.0672  0.0661  0.0688  0.0688
1987-01-22  0.0651  0.0672  0.0672  0.0663  0.0681  0.0681
1987-01-23  0.0652  0.0674  0.0674  0.0656  0.0681  0.0681


df = df.mask(df < 0.1, df * 10)
print (df)
                1      2      3      4      5      6
0                                                   
1987-01-13  0.000  0.657  0.657  0.652  0.678  0.678
1987-01-14  0.630  0.662  0.662  0.653  0.677  0.677
1987-01-15  0.639  0.662  0.662  0.653  0.676  0.676
1987-01-16  0.642  0.663  0.663  0.653  0.679  0.679
1987-01-19  0.644  0.664  0.664  0.658  0.684  0.684
1987-01-20  0.650  0.666  0.666  0.662  0.687  0.687
1987-01-21  0.652  0.672  0.672  0.661  0.688  0.688
1987-01-22  0.651  0.672  0.672  0.663  0.681  0.681
1987-01-23  0.652  0.674  0.674  0.656  0.681  0.681

df.to_csv(file, header=False)

您可以使用:

df = df.applymap(lambda x: x*10 if "0.0" in str(x) else x)

输入:

^{pr2}$

输出:

            a      b      c      d      e      f      g
0  1987-01-13  0.000  0.657  0.657  0.652  0.678  0.678
1  1987-01-14  0.630  0.662  0.662  0.653  0.677  0.677
2  1987-01-15  0.639  0.662  0.662  0.653  0.676  0.676
3  1987-01-16  0.642  0.663  0.663  0.653  0.679  0.679
4  1987-01-19  0.644  0.664  0.664  0.658  0.684  0.684
5  1987-01-20  0.650  0.666  0.666  0.662  0.687  0.687
6  1987-01-21  0.652  0.672  0.672  0.661  0.688  0.688
7  1987-01-22  0.651  0.672  0.672  0.663  0.681  0.681
8  1987-01-23  0.652  0.674  0.674  0.656  0.681  0.681

相关问题 更多 >