2017-04-03 4 views
0

私はJupyter Notebookにデータフレームを提示しています。データフレームの初期データ型はfloatです。私は、印刷された表の行1 & 3を整数として、行2 & 4をパーセンテージとして提示したいと思います。それ、どうやったら出来るの?ここでJupyterノートブックデータフレーム出力の特定の行のフォーマット

#Creating the table 
clms = sales.columns 
indx = ['# of Poeple','% of Poeple','# Purchased per Activity','% Purchased per Activity'] 
basic_stats = pd.DataFrame(data=np.nan,index=indx,columns=clms) 
basic_stats.head() 

#Calculating the # of people who took part in each activity 
for clm in sales.columns: 
    basic_stats.iloc[0][clm] = int(round(sales[sales[clm]>0][clm].count(),0)) 

#Calculating the % of people who took part in each activity from the total email list 
for clm in sales.columns: 
    basic_stats.iloc[1][clm] = round((basic_stats.iloc[0][clm]/sales['Sales'].count())*100,2) 

#Calculating the # of people who took part in each activity AND that bought the product 
for clm in sales.columns: 
    basic_stats.iloc[2][clm] = int(round(sales[(sales[clm] >0) & (sales['Sales']>0)][clm].count())) 

#Calculating the % of people who took part in each activity AND that bought the product 
for clm in sales.columns: 
    basic_stats.iloc[3][clm] = round((basic_stats.iloc[2][clm]/basic_stats.iloc[0][clm])*100,2) 

#Present the table 
basic_stats 

印刷テーブルの: Output table of 'basic_stats' data frame in Jupyter Notebook

+1

可能性のある重複した[列のフォーマット文字列を使用してfloatのパンダのデータフレームを表示するには?](http://stackoverflow.com/questions/20937538/how-to-display-pandas-dataframe-of- floats-using-a-format-string-for-columns) – IanS

+0

テーブルを転置し、提案された複製にソリューションを適用してから、転置して表示することをお勧めします。 – IanS

答えて

0

整数表現

ここ

(ノー成功と解決策を探して、多くの時間を費やしてきた)私が使用しているコードです

すでに行1と3のセルに整数を割り当てています。これらの整数が浮動小数点として出力されるのは、すべての列がデータ型がfloat64であるためです。これは、最初にDataframeを作成する方法によって発生します。あなたは.dtypes属性を印刷することにより、データの種類を表示することができます:

basic_stats = pd.DataFrame(data=np.nan,index=indx,columns=clms) 
print(basic_stats.dtypes) 

# Prints: 
# column1 float64 
# column2 float64 
# ... 
# dtype: object 

あなたがデータ フレームのコンストラクタでdataキーワード引数を指定しない場合、各セルのデータ型ができobjectになります任意のオブジェクト:

basic_stats = pd.DataFrame(index=indx,columns=clms) 
print(basic_stats.dtypes) 

# Prints: 
# column1 object 
# column2 object 
# ... 
# dtype: object 

セルのデータ型がobjectで、内容はそれを使用してフォーマットされ、整数につながる組み込みメソッドが正しくフォーマットBEINです。

割合表現

のパーセンテージを表示するためには、あなたが望む方法フロート番号を印刷したカスタムクラスを使用することができます。

class PercentRepr(object): 
    """Represents a floating point number as percent""" 
    def __init__(self, float_value): 
     self.value = float_value 
    def __str__(self): 
     return "{:.2f}%".format(self.value*100) 

は、それからちょうど行1の値は、このクラスを使用しますそして3:

#Creating the table 
clms = sales.columns 
indx = ['# of Poeple','% of Poeple','# Purchased per Activity','% Purchased per Activity'] 
basic_stats = pd.DataFrame(index=indx,columns=clms) 
basic_stats.head() 

#Calculating the # of people who took part in each activity 
for clm in sales.columns: 
    basic_stats.iloc[0][clm] = int(round(sales[sales[clm]>0][clm].count(),0)) 

#Calculating the % of people who took part in each activity from the total email list 
for clm in sales.columns: 
    basic_stats.iloc[1][clm] = PercentRepr(basic_stats.iloc[0][clm]/sales['Sales'].count()) 

#Calculating the # of people who took part in each activity AND that bought the product 
for clm in sales.columns: 
    basic_stats.iloc[2][clm] = int(round(sales[(sales[clm] >0) & (sales['Sales']>0)][clm].count())) 

#Calculating the % of people who took part in each activity AND that bought the product 
for clm in sales.columns: 
    basic_stats.iloc[3][clm] = PercentRepr(basic_stats.iloc[2][clm]/basic_stats.iloc[0][clm]) 

#Present the table 
basic_stats 

注:これは実際にあなたのデータフレーム内のデータを変更します!行1と3のデータを使用してさらに処理する場合は、これらの行に浮動オブジェクトが含まれていないことに注意してください。

+0

この非常に有用な解決策と以下の重要なコメントをいただきありがとうございます。私は何か新しいことを学んだ:) – Shahar

+0

あなたは大歓迎です:-) – Felix

0

ここでは、ハックのようなものですが、単純にきれいな印刷であれば動作します。

df = pd.DataFrame(np.random.random(20).reshape(4,5)) 

# first and third rows display as integers 
df.loc[0,] = df.loc[0,]*100 
df.loc[2,] = df.loc[2,]*100 

df.loc[0,:] = df.loc[0,:].astype(int).astype(str) 
df.loc[2,:] = df.loc[2,:].astype(int).astype(str) 

# second and fourth rows display as percents (with 2 decimals) 
df.loc[1,:] = np.round(df.loc[1,:].values.astype(float),4).astype(float)*100 
df.loc[3,:] = np.round(df.loc[3,:].values.astype(float),4).astype(float)*100