首页 \ 问答 \ 迭代检查列名是否等于另一列的值(Iterate to check if column name is equal to another column's value)

迭代检查列名是否等于另一列的值(Iterate to check if column name is equal to another column's value)

有没有办法在一个数据框上迭代,如果一个列的名字等于另一列的值,输入一个'1'到特定的单元格中。

例如:

[A]  [B]  [C]  
D
E
B
C

将转化为:

[A] [B] [C]
D
E
B   1   
C       1

B和c中的空值只是NaN。


Is there a way to iterate across a dataframe where if a column name is equal to another column's value to input a '1' into that specific cell.

For example:

[A]  [B]  [C]  
D
E
B
C

would translate to:

[A] [B] [C]
D
E
B   1   
C       1

The null values in B and c would simply be NaN.


原文:https://stackoverflow.com/questions/48408149
更新时间:2024-01-04 16:01

最满意答案

没有我想要的那样优化......但是如果foobarbaz具有通用版本呢?

static bool foo(int input)
{
    return input > 5;
}
static bool foo(string input)
{
    return input.Length > 5;
}
static void baz(int input)
{
    Console.WriteLine(input);
}
static void baz(string input)
{
    Console.WriteLine(input);
}
static bool foo<T>(T input)
{
    if (input is int) return foo((int)(object)input);
    if (input is string) return foo((string)(object)input);
    return false;
}
static void baz<T>(T input)
{
    if (input is int) baz((int)(object)input);
    else if (input is string) baz((string)(object)input);
    else throw new NotImplementedException();
}
static void g<T>(T input)
{
    if (foo(input))
        baz(input);
}
static void g<T, U>(T input, U inputU)
{
    g(input);
    g(inputU);
}

Not as optimal as I would like... but what if foo, bar, and baz had generic versions as well?

static bool foo(int input)
{
    return input > 5;
}
static bool foo(string input)
{
    return input.Length > 5;
}
static void baz(int input)
{
    Console.WriteLine(input);
}
static void baz(string input)
{
    Console.WriteLine(input);
}
static bool foo<T>(T input)
{
    if (input is int) return foo((int)(object)input);
    if (input is string) return foo((string)(object)input);
    return false;
}
static void baz<T>(T input)
{
    if (input is int) baz((int)(object)input);
    else if (input is string) baz((string)(object)input);
    else throw new NotImplementedException();
}
static void g<T>(T input)
{
    if (foo(input))
        baz(input);
}
static void g<T, U>(T input, U inputU)
{
    g(input);
    g(inputU);
}

相关问答

更多
  • object.getClass()返回Class Class ,因此对象应满足该要求,因为它保证是S的子类。 这是正确的,但是你的第二个参数类型是Class而不是Class Class ,不能隐式转换。 在您的示例中,编译器将S替换为用于调用函数的任何声明的类,但它可以是子类。 getClass()返回具体类。 例如,请考虑以下事项: Animal a = new Cat(); postJson(" ...
  • 假设avg方法在接口Averageable中声明,则应将您的类声明为 public class MyClass Assuming the avg method is declared in an interface Averageable, your class should be declared as public class MyClass
  • 没有我想要的那样优化......但是如果foo , bar和baz具有通用版本呢? static bool foo(int input) { return input > 5; } static bool foo(string input) { return input.Length > 5; } static void baz(int input) { Console.WriteLine(input); } static void baz(string input) { C ...
  • 关于第二个例子,编译器不知道你想要什么类型的R; 它只知道它必须实现IRelationship并具有公共默认构造函数。 它无法从您传递给方法的任何参数中推断它,因为它们是T类型。在这种情况下,您需要告诉它要用于R的类。如果您要传入,而是创建一个R的实例,作为一个参数,它将能够推断出类型,你不需要提供它们。 在第一种情况下,您不需要提供类型,因为参数属于类型,因此编译器可以推断出您所指的类型。 With respect to the second example, the compiler does n ...
  • handler是java.lang.Class类型的变量,而不是DefaultHandler (或其任何子类型)。 SAXParser#parse()方法的第二个参数必须是DefaultHandler类型。 您可以尝试使用扩展DefaultHandler的type参数将类声明为泛型类: public class RSSFeeder extends AsyncTask{ private T handler; ...
  • public DateTime ExportResultsToCsv(string filePath, string HeaderLine, List data) { engine = new FileHelperEngine(typeof(T)) { HeaderText = HeaderLine }; engine.WriteFile(filePath, data); return DateTime.Now; } 有关泛型的更多信息, 请参阅MSDN上的 ...
  • 问题不在于缺少更高类型,而是价值限制 :您不能将state定义为StateMonadBuilder>类型的通用值。 由于state是纯粹的,因此单行修复是使state成为类型函数 : let state<'a,'b,'c> = StateMonadBuilder> () 至于World(10,20,30) - World 类型可以采用泛型参数,但union情况不能。 由于类型完全由构造函数的参数决定,因此如果你 ...
  • C#4中的一个选项是使用动态类型: void Transform(T t) { t.Inc(); dynamic d = t; Print(d); } 这将在执行时为您执行重载决策 - 这基本上是您可以做的最好,除非您可以提供真正通用的Print方法,因为只会生成一个版本的Transform 。 请注意,此时并不需要通用 - 您可以将其替换为: void Transform(MyBase t) { ... } 我通常发现基于接口的约束比基于类的约束更有用,除非我还做 ...
  • 我注意到int的唯一引用是在你问题的最后一行。 你确定IEntityService<>只能用于int键吗? 可能它是为复合键 (由多列组成的主键)构建的 现在,在NHibernate中,对于复合键,您使用整个类来表示它们,因此您可以使用表MyTable a class MyTableKey { public int Code; public int SubCode; } 如果您有一个辅助表MuSubtable连接到该表,那么您可以拥有 class MySubtableKey : MyT ...
  • 不是真的,在它的方法中有T参数,T是由你指定的类声明声明的泛型参数。 例如,如果类声明如下所示: class AnotherFileVisitor extends SimpleFileVisitor 方法看起来像这样: visitFile(File file, BasicFileAttributes attrs) 等等... Not really, in its methods there are T parameters, and T being the generic parameter ...

相关文章

更多

最新问答

更多
  • 您如何使用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)