0

我正在尝试在 Python 中对以下 Pandas DataFrame 进行排序:

import numpy as np
import pandas as pd

heading_cols = [
"Video Title",
    "Up Ratings",
    "Down Ratings",
    "Views",
    "User Name",
    "Subscribers",
]
column_1 = [
    "Adelaide",
    "Brisbane",
    "Darwin",
    "Hobart",
    "Sydney",
    "Melbourne",
    "Perth",
]
column_2 = [1295, 5905, 112, 1357, 2058, 1566, 5386]
column_3 = [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]
column_4 = [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]
column_5 = ["Bob","Tom","Dave","Sally","Rick","Mary","Roberta"]
column_6 = [25000,30000,15000,15005,20000,31111,11000]

#Generate data:
xdata_arr = np.array([column_1,column_2,column_3,column_4,column_5,column_6]).T

# Generate the DataFrame:
df = pd.DataFrame(xdata_arr, columns=heading_cols)
print(df)

接下来的 2 行代码会导致问题:

# Print DataFrame and basic stats:
print(df["Up Ratings"].describe())
print(df.sort('Views', ascending=False))

问题:

  • 排序不适用于任何列。
  • 统计信息应包括平均值、标准差、最小值、最大值等。这些不会显示。

问题是 dtypes() 正在为所有列返回“对象”。这是错误的。有些应该是整数,但我不知道如何只更改数字。我努力了:

df.convert_objects(convert_numeric=True)

但这不起作用。所以,然后我去了 NumPy 数组并尝试在那里更改 dtypes:

dt = np.dtype(
[
    (heading_cols[0], np.str_),
    (heading_cols[1], np.int16),
    (heading_cols[2], np.int16),
    (heading_cols[3], np.int16),
    (heading_cols[4], np.str_),
    (heading_cols[5], np.int16),
]

)

但这也不起作用。

有没有办法手动将 dtype 更改为数字?

4

1 回答 1

1

像 pandas 中的大多数方法一样,convert_objects返回一个 NEW 对象。

In [20]: df.convert_objects(convert_numeric=True)
Out[20]: 
  Video Title  Up Ratings  Down Ratings   Views User Name  Subscribers
0    Adelaide        1295       1158259   600.5       Bob        25000
1    Brisbane        5905       1857594  1146.4       Tom        30000
2      Darwin         112        120900  1714.7      Dave        15000
3      Hobart        1357        205556   619.5     Sally        15005
4      Sydney        2058       4336374  1214.8      Rick        20000
5   Melbourne        1566       3806092   646.9      Mary        31111
6       Perth        5386       1554769   869.4   Roberta        11000

In [21]: df.convert_objects(convert_numeric=True).dtypes
Out[21]: 
Video Title      object
Up Ratings        int64
Down Ratings      int64
Views           float64
User Name        object
Subscribers       int64
dtype: object
于 2014-10-10T14:49:48.173 回答