我有一个CSV文件,"value.txt"其中包含以下内容:该文件的前几行是:
Date,"price","factor_1","factor_2"
2012-06-11,1600.20,1.255,1.548
2012-06-12,1610.02,1.258,1.554
2012-06-13,1618.07,1.249,1.552
2012-06-14,1624.40,1.253,1.556
2012-06-15,1626.15,1.258,1.552
2012-06-16,1626.15,1.263,1.558
2012-06-17,1626.15,1.264,1.572
Run Code Online (Sandbox Code Playgroud)
在R中我们可以使用中读取此文件
price <- read.csv("value.txt")
Run Code Online (Sandbox Code Playgroud)
这将返回一个data.frame,我可以用它来进行统计操作:
> price <- read.csv("value.txt")
> price
Date price factor_1 factor_2
1 2012-06-11 1600.20 1.255 1.548
2 2012-06-12 1610.02 1.258 1.554
3 2012-06-13 1618.07 1.249 1.552
4 2012-06-14 1624.40 1.253 1.556
5 2012-06-15 1626.15 1.258 1.552
6 2012-06-16 1626.15 1.263 1.558
7 2012-06-17 1626.15 1.264 1.572
Run Code Online (Sandbox Code Playgroud)
是否有Pythonic方法来获得相同的功能?
我有一个包含多个条目的CSV文件.示例csv:
user, phone, email
joe, 123, joe@x.com
mary, 456, mary@x.com
ed, 123, ed@x.com
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过CSV中的特定列删除重复项,但是下面的代码我得到的"列表索引超出范围".我想通过比较row[1]与 newrows[1]我会找到所有重复,只改写的唯一条目file2.csv.这不起作用,我不明白为什么.
f1 = csv.reader(open('file1.csv', 'rb'))
newrows = []
for row in f1:
if row[1] not in newrows[1]:
newrows.append(row)
writer = csv.writer(open("file2.csv", "wb"))
writer.writerows(newrows)
Run Code Online (Sandbox Code Playgroud)
我的最终结果是有一个列表来维护文件的顺序(set不会工作......对吗?),它应该是这样的:
user, phone, email
joe, 123, joe@x.com
mary, 456, mary@x.com
Run Code Online (Sandbox Code Playgroud) 我有一个myfile.csv像行的行
first, second, third
1, 2, 3
a, b, c
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
等等.
我不明白如何删除重复的行myfile.csv.
一个条件,我们无法保存新文件,我们需要更新myfile.csv.
为了运行后脚本myfile.csv看起来像
first, second, third
a, b, c
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
因此,新数据不会保存到需要更新的新文件中myfile.csv.
非常感谢你.