简体   繁体   English

如何使用python(pandas)更新csv文件中所有行的最后一列值

[英]How to update the last column value in all the rows in csv file using python(pandas)

I am trying to update last column value for all the rows in the csv file using Pandas.我正在尝试使用 Pandas 更新 csv 文件中所有行的最后一列值。 but while updating the value, other column value are dropping.但是在更新值时,其他列值正在下降。

file = r'Test.csv'
# Read the file
df = pd.read_csv(file, error_bad_lines=False)
# df.at[3, "ingestion"] = '20'
df.set_value(1, "ingestion", '30')
df.to_csv("Test.csv", index=False, sep='|')

Use DataFrame.iloc with -1 for select last column and : for select all rows:使用DataFrame.iloc-1来选择最后一列,使用:来选择所有行:

df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

df.iloc[:, -1] = '20'
print (df)
   A  B  C  D  E   F
0  a  4  7  1  5  20
1  b  5  8  3  3  20
2  c  4  9  5  6  20
3  d  5  4  7  9  20
4  e  5  2  1  2  20
5  f  4  3  0  4  20

EDIT:编辑:

For update all rows by last edit value swap -1 with : and get last column value by DataFrame.iat :要通过最后一个编辑值交换-1更新所有行:并通过DataFrame.iat获取最后一列值:

df.iloc[-1, :] = df.iat[-1, -1]
print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  b  b  b  b  b  b

pd.DataFrame.set_value is not appropriate for setting all the values in a column. pd.DataFrame.set_value不适用于设置列中的所有值。 As per the docs, it is used to setting a scalar at a specific row and column label combination.根据文档,它用于在特定的行和列标签组合处设置标量。

Moreover, since v0.21, it has been deprecated in favour of .at / .iat accessors.此外,由于v0.21,它已被弃用,取而代之的.at / .iat存取。

Instead, you can set the value directly by extracting the final column label, assuming you have no duplicate column names:相反,您可以通过提取最终列标签直接设置值,假设您没有重复的列名:

df[df.columns[-1]] = '20'

Or, more directly, you can use the iloc accessor:或者,更直接地,您可以使用iloc访问器:

df.iloc[:, -1] = '20'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新值附加/更新到现有 csv 文件的行中,从新的 csv 文件作为 python 或其他内容的新列 - How to append/update new values to the rows of a existing csv file from a new csv file as a new column in python using pandas or something else 如何使用python中的pandas读取csv文件的所有行? - How to read all rows of a csv file using pandas in python? Python - 比较 a.csv 文件所有行中的最后一列 - Python - Compare last column in all rows of a .csv file 给定使用 python 的最后一列的值,我将如何获得 csv 文件中第一列或第二列的值 - How would I get the value of first or second column in csv file given the value of last column using python 如何按列名过滤值,然后将具有相同值的行提取到另一个CSV文件? Python /熊猫 - How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas 如何在 Python 中使用 Pandas 打印 csv 文件中的所有行/数据? - How to print all rows/data from csv file with Pandas in Python? 如何在python中更新pandas数据框特定列的所有行? - How to update all rows in particular column of pandas dataframe in python? 如何使用 PANDAS python 按单列对 CSV 行进行排序 - How to sort CSV rows by a single column using PANDAS python 如何使用 python Pandas 访问 csv 文件的倒数第二行? - How to access the second to last row of a csv file using python Pandas? How to read every column of a csv file in python after every 10-15 rows which have the same header using pandas or csv? - How to read every column of a csv file in python after every 10-15 rows which have the same header using pandas or csv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM