在Pandas Dataframe中组合文本(Combining Text in Pandas Dataframe)
我有多个列,我想合并在一起,但我希望它由一个表驱动。 所以用户可以指定让我们说字段:'A','B','C'和另一个用户可以说他们想要'A','B','E','Z'等...
所以我希望这在某种意义上是动态的。 另外,在每个行值之间,我需要插入一个
\n
值来获得一个新的行字符。所以在我的字段
description
,给出第一个例子,我需要将字符串格式化为:'A: ' + df['A'] + "\n" + 'B: ' + df['B'] + '\n' + 'C: ' + df['C'] + '\n'
在python中使用字符串数据时,可以利用
+=
操作将文本追加到现有字符串,所以我想我可以在我的Pandas数据帧对象上创建一个名为description
的列,然后使用+=
操作将所有内容组合在一起。df['description'] = "" for f in fields: df['description'] += f + ": " + df[f] + '\n'
但这引发了一个类型错误:
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
关于我如何做到这一点的任何想法?
我使用的是Pandas 0.16.1
谢谢
I have multiple columns that I want to merge together, but I want it driven by a table. So a user can specify let's say fields: 'A', 'B', 'C' and another use can say they want 'A', 'B', 'E', 'Z', etc...
So I want this to be dynamic in a sense. In addition between each row value, I need to insert a
\n
value to have a new line character.So in my field
description
, given the first example, I need the string to be formatted as:'A: ' + df['A'] + "\n" + 'B: ' + df['B'] + '\n' + 'C: ' + df['C'] + '\n'
When working with string data in python, you can leverage the
+=
operation to append text to an existing string, so I thought I could create a column calleddescription
on my Pandas dataframe object and then use the+=
operation to push everything together.df['description'] = "" for f in fields: df['description'] += f + ": " + df[f] + '\n'
But this raises a typeerror:
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
Any ideas on how I can do this?
I am using Pandas 0.16.1
Thanks
原文:https://stackoverflow.com/questions/40975161
最满意答案
对于大多数
UIView
,边界原点总是(0,0)。 但是,UITableView
和UICollectionView
都是UIScrollView
子类。UIScrollView
通过更改其边界原点的x和y值来实现其滚动效果。 这是一篇很好的文章,更深入地解释: http : //oleb.net/blog/2014/04/understanding-uiscrollview/ 。For most
UIView
s, the bounds origin will always be (0,0). However,UITableView
andUICollectionView
are both subclasses ofUIScrollView
.UIScrollView
achieves its scrolling effect by changing the x and y values of the origin of its bounds. Here's a nice article explaining in more depth: http://oleb.net/blog/2014/04/understanding-uiscrollview/.
相关问答
更多-
我认为重写一个继承的属性是Objective-C中可能的,但在Swift中没有(至少目前是)。 我处理这个的方式是将一个单独的委托声明为正确类型的计算属性,该属性获取并设置实际的委托: @objc protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol { func myHeight() -> CGFloat // ... } class MyScrollView: UIScrollView { ...
-
有什么理由需要单独的UIScrollView吗? UITableView已经是一个滚动视图。 我的猜测是你创建的UIScrollView的触摸事件干扰了UITableView的触摸事件。 Is there any reason why you need a separate UIScrollView ? UITableView is already a scroll view. My guess is that the touch events from the UIScrollView you creat ...
-
滚动UITableView和UICollectionView时的数据不正确(Incorrect data on scrolling UITableView and UICollectionView)[2023-06-01]
首先: [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath] 总是返回一个视图,所以你不需要: if(cell==nil) { cell=[[MyCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width, collectionVie ... -
何时使用UICollectionView而不是UITableView?(When to use UICollectionView instead of UITableView?)[2024-02-03]
这取决于要求。 应用程序流程如何确定将哪种类型的UI集成到应用程序中。 人们主要使用UICollectionview来创建具有网格中显示的多个图像的UI类型。 这将使用UITableView具有复杂的逻辑,但是使用UICollectionview ,这将很容易。 当使用UICollectionview ,您不需要通过获取所选项目值来设置带有标签或其他内容的按钮。 你可以简单地获取-(void)collectionView:(UICollectionView *)collectionView didSelec ... -
宽度等于600,因为您在UIView更新当前屏幕的约束之前接受它。 您可以在- (void)viewDidLayoutSubviews方法中检查UICollectionView框架,您将收到实际值。 这有两个选择: 1)您可以独立于UICollectionView框架进行昆虫计算 要么 2)当viewDidLayoutSubviews触发并重绘UICollectionView后,您可以重新计算集合视图布局 (我宁愿第一个) Width equals 600 because you take it befor ...
-
我在tableview上使用“hitTest”修复它。 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 如果事件源自tableview,我将禁用父级的scrollview滚动。 并且,当tableview的滚动结束时(scrollViewDidEndDragging),我重新启用父级的滚动视图滚动。 接缝工作正常。 I fixed it using "hitTest" on the tableview. - (UIView * ...
-
这对我有用:请看附带的GIF。 我添加了与scrool指示器平行的蓝色自定义uiview。 我尝试了表视图,但这也适用于集合视图。 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ dispatch_async(dispatch_get_main_queue(), ^{ //get refrence of vertical indicator UIImageView *verticalIndicator = ...
-
我刚刚遇到了类似的情况(UIScrollView - > UITableView)并且几个小时都在敲打它。 简短的回答是:在我的ViewController(管理UIScrollView)被发送到viewWillAppear:之前,用作调整大小的基础的正确大小是不可用的。 在此之前,我发现值已设定,但往往是错误的。 (例如,我第一次尝试这样做是在viewDidLoad: ,并且在那里更改了大小调整值,但它们是错误的 !) 所以我的解决方案是: 在viewDidLoad:一旦你有数据,你的封闭的UITable ...
-
对于大多数UIView ,边界原点总是(0,0)。 但是, UITableView和UICollectionView都是UIScrollView子类。 UIScrollView通过更改其边界原点的x和y值来实现其滚动效果。 这是一篇很好的文章,更深入地解释: http : //oleb.net/blog/2014/04/understanding-uiscrollview/ 。 For most UIViews, the bounds origin will always be (0,0). However ...
-
同意@Piotr菜单必须是表的委托,所以替换self.table.delegate = delegate; 与self.table.delegate = self; 在MenuView.m中。 但另外,MenuView.m永远不会调用它的委托 ,它应该在tableview中选择.... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self hideDrop ...