首页 \ 问答 \ 对于具有numpy / pandas的循环(迭代列标题)(For loops with numpy/pandas (iterating over column headers))

对于具有numpy / pandas的循环(迭代列标题)(For loops with numpy/pandas (iterating over column headers))

我有一个带头的熊猫数据框(玩家)(传球,铲球......)

我希望获得一些列的相关系数(不是全部)

for stats in [passes,tackles,shots,saves]:
    cc = np.corrcoef(players.minutes, players.stats)[1,0]
    print cc

但是,我的代码返回

name 'passes' is not defined

因为传递不是定义的变量。 是否有一种简单的方法来迭代某些列以进行此类分析?


I have a pandas dataframe (players) with headers (passes, tackles....)

I am looking to get a correlation coefficient for a few of the columns (not all)

for stats in [passes,tackles,shots,saves]:
    cc = np.corrcoef(players.minutes, players.stats)[1,0]
    print cc

However, my code returns

name 'passes' is not defined

because passes is not a defined variable. Is there an easy way to iterate over certain columns for this type of analysis?


原文:https://stackoverflow.com/questions/50437942
更新时间:2022-08-08 19:08

最满意答案

如何使用Windows命令行环境查找和替换文件中的文本? 列出了几个用于从命令行替换文件中的字符串的工具。

我喜欢免费的控制台工具xchang32.exe,因为它功能强大且非常易于使用。

我将XML文件的内容复制到一个文件中,并使用名称Test.xml保存它。

然后我执行了以下命令:

xchang32.exe Test.xml "port=^34third^34 protocol" "port=^34newPort^34 protocol"

该命令仅在文件中更改

<Connector port="third" protocol="HTTP/1.1" SSLEnabled="..." /> 

<Connector port="newPort" protocol="HTTP/1.1" SSLEnabled="..." /> 

^34是双引号字符的转义序列" ,其具有十进制代码值34。

xchang32.exe /? 在命令提示符窗口中输入并执行此替换工具的输出帮助。

当然,您也可以使用引用主题中建议的任何其他方法。 您只需要在搜索中包含协议并替换字符串以仅修改应修改的端口号并忽略所有其他端口号。 仅使用Windows标准命令的解决方案更加困难,因为XML文件中的字符<>"在命令行中具有特殊含义。


编辑:

当然知道在执行批处理文件时不知道文件中的当前端口号是一个重要的事实。 从最初的问题来看,这对我来说并不是很清楚。 这使得它更加困难。

一种解决方案是首先搜索现有端口值,一旦找到就进行替换。

@echo off

rem Search in file for a line where first opening tag is
rem "Connector" which has as first attribute "port" and
rem one more attribute "protocol" and assign the value
rem of the attribute port to an environment variable.

for /F "tokens=1-4 delims==<     " %%A in (Test.xml) do (
    if "%%A"=="Connector" (
        if "%%B"=="port" (
            if "%%D"=="protocol" (
                set "PortNumber=%%~C"
                goto Replace
            )
        )
    )
)
echo Could not find the port value of interest.
goto :EOF

:Replace
xchang32.exe Test.xml "port=^34,%PortNumber%^34 protocol" "port=^34,%~1^34 protocol"
set "PortNumber="

注意:

在使用以下命令后,在delims= on行之后有4个字符:

  1. 一个等号,
  2. 一个左尖括号,
  3. 一个水平制表符(不是浏览器显示的空格序列)
  4. 单个空格字符。

分隔符的顺序很重要,否则命令行解释器会因语法错误而退出批处理脚本。

感兴趣的行将命令分为4个分隔符:

  • 令牌1: Connector
  • 令牌2: port
  • 令牌3: "third"
  • 令牌4: protocol
  • 还有更多不感兴趣的代币。

这4个标记分配给循环变量A (标记名称), B (第一个属性的名称), C (第一个属性的值)和D (第二个属性的名称)。

变量ABD的值用于检查是否找到了正确的行。 变量C的值在肯定检查时分配给环境变量,并删除周围的双引号。 然后通过跳转退出循环以替换部分批处理脚本。

再次使用免费工具xchang32.exe在文件中进行替换而不更改任何其他内容。 由于赋给属性端口的值是整数值,因此必须在第一个^34之后使用逗号让工具知道引用双引号字符的整数在何处结束。

有关在命令提示符窗口中执行命令的帮助,可以for /?help for


How can you find and replace text in a file using the Windows command-line environment? lists several tools for replacing a string in a file from command line.

I like free console tool xchang32.exe as it is powerful and very easy to use.

I copied the content of your XML file into a file and saved it with name Test.xml.

Then I executed following command:

xchang32.exe Test.xml "port=^34third^34 protocol" "port=^34newPort^34 protocol"

That command changed in the file only

<Connector port="third" protocol="HTTP/1.1" SSLEnabled="..." /> 

to

<Connector port="newPort" protocol="HTTP/1.1" SSLEnabled="..." /> 

^34 is an escape sequence for double quote character " which has decimal code value 34.

xchang32.exe /? entered and executed in a command prompt window outputs help for this replace tool.

Of course you could use also any other method suggested in the referenced topic. You just need to include protocol in search and replace string to modify just the port number which should be modified and ignore all other. Solutions using only Windows standard commands are more difficult because of the characters <, > and " in the XML file which have a special meaning on command line.


EDIT:

It is of course an important fact to know that the current port number in file is not known on executing the batch file. That was not really clear for me from the initial question. This makes it more difficult.

One solution would be to first search for existing port value and once found do the replace.

@echo off

rem Search in file for a line where first opening tag is
rem "Connector" which has as first attribute "port" and
rem one more attribute "protocol" and assign the value
rem of the attribute port to an environment variable.

for /F "tokens=1-4 delims==<     " %%A in (Test.xml) do (
    if "%%A"=="Connector" (
        if "%%B"=="port" (
            if "%%D"=="protocol" (
                set "PortNumber=%%~C"
                goto Replace
            )
        )
    )
)
echo Could not find the port value of interest.
goto :EOF

:Replace
xchang32.exe Test.xml "port=^34,%PortNumber%^34 protocol" "port=^34,%~1^34 protocol"
set "PortNumber="

Attention:

There are 4 characters after delims= on line with command for:

  1. an equal sign,
  2. a left angle bracket,
  3. a horizontal tab character (not a sequence of spaces as browser displays) and
  4. a single space character.

The order of the delimiter characters is important as otherwise command line interpreter would exit batch script because of a syntax error.

The line of interest is split up by command for with those 4 delimiters into:

  • token 1: Connector
  • token 2: port
  • token 3: "third"
  • token 4: protocol
  • and more tokens not of interest.

Those 4 tokens are assigned to the loop variables A (tag name), B (name of first attribute), C (value of first attribute) and D (name of second attribute.

The values of variables A, B and D are used to check if the right line was found. The value of variable C is assigned on positive check to an environment variable with removing the surrounding double quotes. Then the loop is exited with a jump to replace part of the batch script.

Again free tool xchang32.exe is used to make the replacement in the file without changing anything else. As the value assigned to attribute port is an integer value, it is necessary to use a comma after first ^34 to let the tool know where the integer number referencing the double quote character ends.

For help on command for execute in a command prompt window either for /? or help for.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)