Python:使用 Pandas,如何选择输出中的列?

Python: Using Pandas, how do I choose the columns in my output?

我运行将我的整个 Active 目录与试图查找不属于的用户帐户进行比较。 使用我的代码,我的输出给出了在用户名列中只出现一次的单词。即使我正在分析一列数据,我也想保留所有包含数据的列。

from pandas import DataFrame, read_csv
import pandas as pd  
f1 = pd.read_csv('lastlogonuser.txt', sep='\t', encoding='latin1')
f2 = pd.read_csv('UserAccounts.csv', sep=',', encoding ='latin1')
f2 = f2.rename(columns={'Shortname':'User Name'})
f = pd.concat([f1, f2])
counts = f['User Name'].value_counts()
f = counts[counts == 1] 
f 

当我 运行 我的代码时,我得到了这样的东西:

sample534         1
sample987         1
sample342         1
sample321         1
sample123         1

我希望 txt 文件中的所有数据都出现在我的输出中,但我仍然只想分析用户名列。我如何保留所有列中的所有数据,或者我是否必须使用不同的字数来包含所有数据列?

我想要这样的东西:

   User Name    Description
1  sample534    Journal Mailbox managed by         
1  sample987    Journal Mailbox managed by    
1  sample342    Journal Mailbox managed by   
1  sample321    Journal Mailbox managed by 
1  sample123    Journal Mailbox managed by 

我使用的数据样本:

Account User Name User CN                       Description
ENABLED MBJ29     CN=MBJ29,CN=Users             Journal Mailbox managed by  
ENABLED MBJ14     CN=MBJ14,CN=Users             Journal Mailbox managed by
ENABLED MBJ08     CN=MBJ30,CN=Users             Journal Mailbox managed by   
ENABLED MBJ07     CN=MBJ07,CN=Users             Journal Mailbox managed by 

根据您的描述,我猜您想使用唯一元素的计数作为数据框中 select 行的索引。也许你可以试试这个:

df2 = pd.DataFrame()    
counts = f['User Name'].value_counts()
counts = counts[counts == 1].index
for index in counts:
    df2 = df2.append(f[f['User Name'] == index])