小编Har*_*les的帖子

Pandas将数据帧列写入csv

我正在编写一个脚本来将带有标头的大型.xlsx文件缩减为csv,然后编写一个新的csv文件,其中只包含基于标题名称的必需列.

import pandas
import csv

df = pandas.read_csv('C:\\Python27\\Work\\spoofing.csv')

time = df["InviteTime (Oracle)"]
orignum = df["Orig Number"]
origip = df["Orig IP Address"]
destnum = df["Dest Number"]

df.to_csv('output.csv', header=[time,orignum,origip,destnum])
Run Code Online (Sandbox Code Playgroud)

我得到的错误是最后一段代码,它说

ValueError: Writing 102 cols but got 4 aliases
Run Code Online (Sandbox Code Playgroud)

我确定我忽略了一些愚蠢的东西,但我已经阅读了熊猫网站上的to_csv文档,我仍然不知所措.我知道我正在使用to_csv参数不正确,但我似乎无法理解我的文档.

任何帮助表示赞赏,谢谢!

python csv pandas

25
推荐指数
1
解决办法
7万
查看次数

类型错误:'in <string>' 需要字符串作为左操作数而不是列表

我正在完成 Learn Python The Hard Way (LPTHW) 的练习 35。

http://learnpythonthehardway.org/book/ex35.html

我正在为这项练习的一项学习练习而苦苦挣扎。特别是“向游戏中添加更多内容”。

这是返回错误的代码:

def bear_room(): # A new encounter
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"

    bear_moved = False 

while True: 
    next = str(raw_input("> "))

    if ["honey", "take"] in next: 
        dead("The bear peers at you for a moment, sizing you up, then claws your face …
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
2万
查看次数

从LPTHW故障理解循环练习39

我正在努力学习Python The Hard Way,我在理解和查找信息时遇到了一些问题.

这是代码.

statesAbbrev = {
"Oregon" : "OR",
"Florida" : "FL",
"California" : "CA",
"New York" : "NY",
"Michigan" : "MI",
"Washington" : "WA"
}

statesCities = {
"CA" : "San Francisco",
"MI" : "Detroit",
"FL" : "Jacksonville",
"WA" : "Seattle",
"NY" : "Buffalo"
}

print "-" * 10
for state, abbrev in statesAbbrev.items():  
    print "%s is abbreviated %s" % (state, abbrev)
Run Code Online (Sandbox Code Playgroud)

我根本不理解这个循环.我在看它,它让我的大脑融化.我明白statesAbbrev.items()可能抓住了那个dict中的第一个项目,但是'state'还没有定义,'abbrev'是什么?另外for循环如何调用'something,somethingElse'

请帮我理解!

您可以在此处找到整个练习和所有代码:http://learnpythonthehardway.org/book/ex39.html

python python-2.7

0
推荐指数
1
解决办法
1510
查看次数

标签 统计

python ×3

csv ×1

pandas ×1

python-2.7 ×1