[英]How do I remove name and dtype from pandas output
我有 output 看起來像這樣:
nutrition_info_256174499 = df1.loc[:"Salt" , "%Reference Intake*"]
print (nutrition_info_256174499)
Typical Values
Energy 5%
Fat 1%
of which saturates 1%
Carbohydrates 7%
of which sugars 2%
Fibre -
Protein 7%
Salt 6%
Name: %Reference Intake*, dtype: object
必須做些什么來刪除 output 末尾的 Name 和 dtype?
對於保留索引的打印,您可以使用.to_string()
:
df = pd.DataFrame({'a': [1,2,3], 'b': [2.23, 0.23, 2.3]}, index=['x1', 'x2', 'x3'])
s = df.loc[:'x2', 'b']
type(s)
# Out: pandas.core.series.Series
print(s)
# Out:
x1 2.23
x2 0.23
Name: b, dtype: float64 # <- OP is asking to remove "name and dtype"
# solution:
print(s.to_string())
# Out:
x1 2.23
x2 0.23
.values
屬性。例子:
s = pd.Series(['race','gender'],index=[1,2])
print(s)
Out[159]:
1 race
2 gender
dtype: object
s.values
array(['race', 'gender'], dtype=object)
您可以轉換為列表或訪問每個值:
list(s)
['race', 'gender']
print(nutrition_info_256174499.to_string())
應該刪除 Name: %Reference Intake*, dtype: object in your prints
只需使用.tolist()
將數據幀值轉換為列表即可刪除 dtype 元素。 在您可以遍歷列表以獲取單個值之后:
df_as_a_list = df.values.tolist()
也許這不是您想要的,但是對於它的價值,如果您將列名放在列表中,您會得到 DataFrame 而不是系列,名稱在頂部而不是底部並且沒有 dtype。
df1.loc[:"Salt", ["%Reference Intake*"]]
%Reference Intake*
Typical Values
Energy 5%
Fat 1%
of which saturates 1%
Carbohydrates 7%
of which sugars 2%
Fibre -
Protein 7%
Salt 6%
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.