Python Pandas df.Unique display/print 内容
Python Pandas df.Unique display/print contents
我是 python 的新手,当我尝试在 excel 列中显示根据唯一值制作的数据框时,我 运行 遇到了一个问题。
所以发生的事情是我尝试写 "aa" 我得到
[u'a' u'b' u'c' u'd' u'e' u'f']
当我想要的是
a, b, c, d, e, f
甚至
[a,b,c,d,e,f]
以更直接的为准。我的想法是,因为我使用的是 str()
,所以它按原样使用数据框,但是,如果我在编写它时不包含 str()
,我会得到
∞*
p!`!@‹!@˛
作为输出...
这是我的代码:
df = pd.read_excel(open('/Users/keatonmaclean/Desktop/abcc.xlsx','rb'), sheetname='Sheet1')
# Set ipython's max row display
pd.set_option('display.max_row', 1000)
# Set iPython's max column width to 50
pd.set_option('display.max_columns', 50)
df.columns = df.iloc[0]
df = df[1:]
aa = str(df.loc[:,"Supplier"].unique())
#bb = str(df.loc[:,"CT #"].unique())
#cc = str(df.loc[:,"CT DESC"].unique())
#dd = str(df.loc[:,"CT START"].unique())
#ee = str(df.loc[:,"CT END"].unique())
import os.path
save_path = '/Users/keatonmaclean/Desktop/'
#name_of_file = raw_input("What is the name of the file: ")
name_of_file = "test"
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w+")
toFile = aa
file1.write(toFile)
file1.close()
我认为你需要 DataFrame
或 Series
构造函数 DataFrame.to_csv
or Series.to_csv
:
aa = np.array(['a','b','c','d','e','f'])
df = pd.DataFrame([aa])
print (df)
0 1 2 3 4 5
0 a b c d e f
df.to_csv(file, index=False)
或者如果需要一栏:
s = pd.Series(aa)
print (s)
0 a
1 b
2 c
3 d
4 e
5 f
dtype: object
s.to_csv(file, index=False)
但是如果需要从所有唯一值创建文件是可能的 apply
函数 unique
.
但是如果每列不同长度的唯一值得到 NaN
s 并且在输出中 NaN
s 被替换为空 space.
df = pd.DataFrame({'Supplier':list('abcceb'),
'CT #':[4,5,4,5,5,4],
'CT DESC':[7,8,9,4,2,3],
'CT START':[1,3,5,7,1,0],
'CT END':[5,3,6,9,2,4]})
print (df)
CT # CT DESC CT END CT START Supplier
0 4 7 5 1 a
1 5 8 3 3 b
2 4 9 6 5 c
3 5 4 9 7 c
4 5 2 2 1 e
5 4 3 4 0 b
df = df.apply(lambda x: pd.Series(x.unique())).astype(object)
print (df)
CT # CT DESC CT END CT START Supplier
0 4 7 5 1 a
1 5 8 3 3 b
2 NaN 9 6 5 c
3 NaN 4 9 7 e
4 NaN 2 2 0 NaN
5 NaN 3 4 NaN NaN
df.to_csv(file, index=False)
我是 python 的新手,当我尝试在 excel 列中显示根据唯一值制作的数据框时,我 运行 遇到了一个问题。
所以发生的事情是我尝试写 "aa" 我得到
[u'a' u'b' u'c' u'd' u'e' u'f']
当我想要的是
a, b, c, d, e, f
甚至
[a,b,c,d,e,f]
以更直接的为准。我的想法是,因为我使用的是 str()
,所以它按原样使用数据框,但是,如果我在编写它时不包含 str()
,我会得到
∞*
p!`!@‹!@˛
作为输出...
这是我的代码:
df = pd.read_excel(open('/Users/keatonmaclean/Desktop/abcc.xlsx','rb'), sheetname='Sheet1')
# Set ipython's max row display
pd.set_option('display.max_row', 1000)
# Set iPython's max column width to 50
pd.set_option('display.max_columns', 50)
df.columns = df.iloc[0]
df = df[1:]
aa = str(df.loc[:,"Supplier"].unique())
#bb = str(df.loc[:,"CT #"].unique())
#cc = str(df.loc[:,"CT DESC"].unique())
#dd = str(df.loc[:,"CT START"].unique())
#ee = str(df.loc[:,"CT END"].unique())
import os.path
save_path = '/Users/keatonmaclean/Desktop/'
#name_of_file = raw_input("What is the name of the file: ")
name_of_file = "test"
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w+")
toFile = aa
file1.write(toFile)
file1.close()
我认为你需要 DataFrame
或 Series
构造函数 DataFrame.to_csv
or Series.to_csv
:
aa = np.array(['a','b','c','d','e','f'])
df = pd.DataFrame([aa])
print (df)
0 1 2 3 4 5
0 a b c d e f
df.to_csv(file, index=False)
或者如果需要一栏:
s = pd.Series(aa)
print (s)
0 a
1 b
2 c
3 d
4 e
5 f
dtype: object
s.to_csv(file, index=False)
但是如果需要从所有唯一值创建文件是可能的 apply
函数 unique
.
但是如果每列不同长度的唯一值得到 NaN
s 并且在输出中 NaN
s 被替换为空 space.
df = pd.DataFrame({'Supplier':list('abcceb'),
'CT #':[4,5,4,5,5,4],
'CT DESC':[7,8,9,4,2,3],
'CT START':[1,3,5,7,1,0],
'CT END':[5,3,6,9,2,4]})
print (df)
CT # CT DESC CT END CT START Supplier
0 4 7 5 1 a
1 5 8 3 3 b
2 4 9 6 5 c
3 5 4 9 7 c
4 5 2 2 1 e
5 4 3 4 0 b
df = df.apply(lambda x: pd.Series(x.unique())).astype(object)
print (df)
CT # CT DESC CT END CT START Supplier
0 4 7 5 1 a
1 5 8 3 3 b
2 NaN 9 6 5 c
3 NaN 4 9 7 e
4 NaN 2 2 0 NaN
5 NaN 3 4 NaN NaN
df.to_csv(file, index=False)