keyerror in simple dataset regplot - AttributeError: 'str' object has no attribute 'conjugate'
keyerror in simple dataset regplot - AttributeError: 'str' object has no attribute 'conjugate'
我正在创建此数据:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
data = np.array([['', 'Gross ', 'Target'],
[0, 728, 500],
[1, 701, 750],
[2, 590, 570],
[3, 548, 596]])
df = pd.DataFrame(data=data[1:, 1:], columns=data[0, 1:])
df
Gross Target
0 728 500
1 701 750
2 590 570
3 548 596
并尝试绘制:
sns.regplot(x='Gross', y='Target', data=df)
结果:KeyError: 'Gross'
--- 更新 ---
修复空白后,我收到:
AttributeError: 'str' object has no attribute 'conjugate'
字符串中有whitespace
,转换为列:
print (df.columns.tolist())
['Gross ', 'Target']
一个可能的解决方案是 strip
:
df.columns = df.columns.str.strip()
print (df.columns.tolist())
['Gross', 'Target']
或在列名中添加空格:
sns.regplot(x='Gross ', y='Target', data=df)
print (df.dtypes)
Gross object
Target object
dtype: object
sns.regplot(x='Gross', y='Target', data=df.astype(int))
创建数组 data
时,numpy 会将数字转换为字符串。 seaborn 试图用这些字符串做数学运算,但它不起作用。如果您将列标题放在一个数组中,将数字放在另一个数组中,它将起作用。
我正在创建此数据:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
data = np.array([['', 'Gross ', 'Target'],
[0, 728, 500],
[1, 701, 750],
[2, 590, 570],
[3, 548, 596]])
df = pd.DataFrame(data=data[1:, 1:], columns=data[0, 1:])
df
Gross Target
0 728 500
1 701 750
2 590 570
3 548 596
并尝试绘制:
sns.regplot(x='Gross', y='Target', data=df)
结果:KeyError: 'Gross'
--- 更新 ---
修复空白后,我收到:
AttributeError: 'str' object has no attribute 'conjugate'
字符串中有whitespace
,转换为列:
print (df.columns.tolist())
['Gross ', 'Target']
一个可能的解决方案是 strip
:
df.columns = df.columns.str.strip()
print (df.columns.tolist())
['Gross', 'Target']
或在列名中添加空格:
sns.regplot(x='Gross ', y='Target', data=df)
print (df.dtypes)
Gross object
Target object
dtype: object
sns.regplot(x='Gross', y='Target', data=df.astype(int))
创建数组 data
时,numpy 会将数字转换为字符串。 seaborn 试图用这些字符串做数学运算,但它不起作用。如果您将列标题放在一个数组中,将数字放在另一个数组中,它将起作用。