替换数据框中特定列中的唯一值(Replace unique values in specific columns in a dataframe)
我的数据框看起来像这样:
col1 col2 col3 Aba xxx yyy bab bhh jjj ccc kkk lll Aba xxx yyy ccc kkk jjj
目前我正在替换每列的所有唯一值:
在
col1
:Aba
被替换为a0
,bab
被替换为a1
,ccc
被替换为a2
,无论它出现在列中。与
col2
类似:xxx
被替换为b0
,bhh
被替换为b1
等。简而言之,第一列开始用
a0,a1,a2,a3
第二个用b0,b1,b2,b3
替换唯一值,用c0,c1,c2
等替换第三列 - 使用这个简单的单行,import string df = list(string.ascii_lowercase)[:len(df.columns)] + df.apply(lambda x: pd.factorize(x)[0]).astype(str)
但是我想像上面的方法那样替换只有那些具有多个唯一值(
nunique()
函数)的列小于假设50的列,并且列的其余部分的值可以保持nunique()
。使用上面的代码寻找解决方案,可以更改为包含此目标。 我的数据框有数百万行和
20
列。谢谢
My dataframe looks like this:
col1 col2 col3 Aba xxx yyy bab bhh jjj ccc kkk lll Aba xxx yyy ccc kkk jjj
Currently I am replacing all the unique values of each column for ex:
In
col1
:Aba
gets replaced witha0
,bab
gets replaced witha1
,ccc
gets replaced witha2
where ever it appears in the column.Similarly with
col2
:xxx
gets replaced withb0
,bhh
gets replaced withb1
etc.In short first column starts replacing unique values with
a0,a1,a2,a3
2nd one withb0,b1,b2,b3
, third column withc0,c1,c2
.. etc - using this simple one-liner,import string df = list(string.ascii_lowercase)[:len(df.columns)] + df.apply(lambda x: pd.factorize(x)[0]).astype(str)
But I want to replace like this above method to only those columns which has number of unique values (
nunique()
function in pandas) lesser than suppose 50 and the values of rest of the column can stay as they are.Looking for a solution using the above piece of code which can be changed to include this objective. My dataframe has millions of rows and more than
20
columns.Thanks
原文:https://stackoverflow.com/questions/49212552
最满意答案
在listview的
DragEnter
事件中,返回以下DragDropEffects
:private void listView_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; }
要在
DragDrop
事件中提取和读取Outlook消息,我建议使用此库。 这是非常容易使用:private void listView_DragDrop(object sender, DragEventArgs e) { OutlookDataObject dataObject = new OutlookDataObject(e.Data); //get the names and data streams of the files dropped string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor"); MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents"); for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++) { string filename = filenames[fileIndex]; MemoryStream filestream = filestreams[fileIndex]; OutlookStorage.Message message = new OutlookStorage.Message(filestream); // do whatever you want with "message" message.Dispose(); } }
In the listview's
DragEnter
Event, return the followingDragDropEffects
:private void listView_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; }
To extract and read the Outlook message(s) within the
DragDrop
event, I recommend using this library. It is very easy to use:private void listView_DragDrop(object sender, DragEventArgs e) { OutlookDataObject dataObject = new OutlookDataObject(e.Data); //get the names and data streams of the files dropped string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor"); MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents"); for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++) { string filename = filenames[fileIndex]; MemoryStream filestream = filestreams[fileIndex]; OutlookStorage.Message message = new OutlookStorage.Message(filestream); // do whatever you want with "message" message.Dispose(); } }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
简短的答案是否 - 此功能需要使用Javascript。 长答案 - 是(以不同的方式) - 您可以在按钮网址上使用唯一的查询字符串(例如?USER=1&COUPON_CODE=123 ),这样您就可以自动将唯一优惠券代码从电子邮件中携带到着陆页表单字段。 要做到这一点,您可以在CSV文件中为您的数据库中的每个成员分配一个唯一的优惠券(或任何您使用的),并在您的ESP中设置一个合并字段,并使用它来自动创建字符串。 例如,你的字符串看起来像这样,当你编写它时,你的ESP将完成剩下的工作,只要数据库设置正确。 ...
-
从剪贴板中获取Word ML(Get Word ML from clipboard)[2023-02-09]
将流保存到磁盘并与正确的docx进行二进制比较后,我发现它实际上包含在一个复合文档文件中: http : //www.openoffice.org/sc/compdocfileformat.pdf 。 我用谷歌搜索了前几个字节 D0 CF 11 E0 A1 B1 1A E1 这是CDF格式的标识符。 可以使用OpenMCDF从Compound文件中提取包。 After saving the stream to disk and doing a binary comparison to a proper d ... -
在listview的DragEnter事件中,返回以下DragDropEffects : private void listView_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; } 要在DragDrop事件中提取和读取Outlook消息,我建议使用此库。 这是非常容易使用: private void listView_DragDrop(object sender, DragEventArgs ...
-
将文件从硬盘驱动器复制到剪贴板时,它只是以CF_HDROP格式复制文件的完整路径和文件名。 您可以使用DragQueryFile()函数来读取文件名,例如: uses Classes, Clipbrd, ShellAPI; var hDrop: THandle MyStream: TMemoryStream; Files: TStringList; NumFiles, FileIdx: DWORD; FileName: array[0..MAX_PATH] of Char; I ...
-
复制到剪贴板并维护电子邮件签名的HTML元素(Copy to Clipboard and Maintain HTML elements for Email signature)[2023-09-15]
当copy事件触发时,您需要指示浏览器以text/html传递文本。 我已经重写了您的代码片段以包含此功能。 function copyToClipboard(element) { var $temp = $(""); $("body").append($temp); $temp.val($(element).html()).select(); var str = $(element).html(); function listener(e) { ... -
如何知道用户何时将某些东西复制到android中的剪贴板?(How to know when user copied something to clipboard in android?)[2023-10-03]
是 您可以在Android中使用ClipboardManager类中的OnPrimaryClipChangedListener 。 看看这个 编辑:示例: class ClipboardListener implements ClipboardManager.OnPrimaryClipChangedListener { public void onPrimaryClipChanged() { // use getPrimaryClip() to get the data or si ... -
这里有代码将光标移动到末尾http://www.vboffice.net/en/developers/determine-cursor-position/ Public Sub SetCursor() Dim Ins As Outlook.Inspector Dim Doc As Word.Document Dim range As Word.range Dim pos As Long Set Ins = Application.ActiveInspector ...
-
实际上,工具提示不是clipboard.js的一部分。 如果查看页面的源代码,可以看到在tooltip.js中添加并配置了demo.js 。 您需要做的就是添加工具提示库或函数,就像演示页面一样,并在按钮点击时使用它。 希望能帮助到你。 Actually, the tooltip is not a part of the clipboard.js. If you look at the source code of the page, you can see that tooltip.js is been ...
-
从VB.NET中的电子邮件附件获取复制到剪贴板的文件名(Getting name of file copied to clipboard from email attachment in VB.NET)[2021-12-26]
事实证明,您可以从格式为FileGroupDescriptor的数据对象中获取文件名。 但代码有点神秘: Dim fileName As New StringBuilder("") Dim theStream As Stream = Clipboard.GetData("FileGroupDescriptor") Try Dim fileGroupDescriptor(512) As Byte theStream.Read(fileGroupDescriptor, 0, 512) ...