簡體   English   中英

Python 3:迭代列的每個值的最佳方法是什么?

[英]Python 3:what is the best way to iterate for each value of a column?

我是 Python 的新手,想就迭代給定數據列的最簡單方法提出一些建議。

我的輸入文件如下所示:

Col1,Col2,Col3<br/>
593457863416,959345934754,9456968345233455<br/>
487593748734,485834896965,4958558475345<br/>
694568245543,34857495345,494589589209<br/>

...

我想做的是將 100 添加到第 2 列中的所有項目。所以 output 會這樣:

Col1,Col2,Col3<br/>
593457863416,959345934854,9456968345233455<br/>
487593748734,485834897065‬,4958558475345<br/>
694568245543,34857495445,494589589209<br/>

...

到目前為止,這是我的代碼:

import csv

with open("C:/Users/r00t/Desktop/test/sample.txt") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    output_list = []
    for row in csv_reader:
        if line_count == 0:
            print(f'{", ".join(row)}')
            line_count += 1
        else:
            temp_list = []
            output_row = int(row[1])
            output_row = output_row + 100
            temp_list =[row[0], row[1], row[2]]
            output_list = [[row[0], output_row, row[2]]]
            print(output_list)
            line_count += 1

代碼似乎不是最優的。 有沒有辦法不為行指定索引? 當我的文件超過 3 列時會發生什么情況?

謝謝! -r

我建議使用csv.DictReader() 每行現在都在字典中,鍵是列名,值是行值。

這里我使用了 pandas

import pandas as pd

df = pd.read_csv('C:/Users/r00t/Desktop/test/sample.txt')
# df1 = df+100

編輯-1

df['col2'] = df['col2'] + 100

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.add.html

這是有關如何操作的建議。 使用 pandas 處理數據非常方便。

import pandas as pd

df = pd.read_csv("sample.txt")
print(df)

# I am basicly getting all the rows of column index 1 (wich is the Col2)
df.iloc[:, 1] = df.iloc[:, 1] + 100
print(df)

# I could also use the column name
df["Col3"] = df["Col3"] + 1

在此處輸入圖像描述

您可以使用基於系列的增值。 或者您可以使用位置,也可以使用 pandas 就地更新。

最簡單的方法(在熊貓中)

df["column2"] = df["column2"] + 100  

ILocation(在熊貓中)

df.iloc[:, 1] = df.iloc[:, 1] + 100

無 Pandas

file_read = csv.reader(open('/tmp/test.csv'))
file_data_in_list = list(file_read)
# Since now you have three columns,
# you can just simply go through 1st index and add 1 there 
for index in range(len(file_data_in_list):
    if index > 0:
        file_data_in_list[index][1] += 100 # Adds hundred to each line of 2nd column. 
# Now you can use file_data_in_list, it won't require you extra variables and the replacment is in place.

IT 最好為此操作使用基於列的數據結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM