我的df1索引df1.index.shape
为(80,)
。我有一个numpy数组df2
,df2.shape
(80,2)
。但是,当我尝试将df2
转换为数据框df2 = pd.DataFrame(df2,index=df1.index)
时,我收到以下错误消息:
ValueError:传递值的形状为(80,2),index islyly(80,80)
为什么我收到此错误?任何帮助表示赞赏。
答案 0 :(得分:0)
对我来说它很好用:
import pandas as pd
df1 = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6]}, index=['a','b','c'])
print (df1)
A B
a 1 4
b 2 5
c 3 6
print (df1.index.shape)
(3,)
df2 = df1.values
print (df2)
[[1 4]
[2 5]
[3 6]]
print (df2.shape)
(3, 2)
print (pd.DataFrame(df2,index=df1.index))
0 1
a 1 4
b 2 5
c 3 6
但如果将index
更改为columns
,则会引发错误:
print (pd.DataFrame(df2,columns=df1.index))
ValueError: Shape of passed values is (2, 3), indices imply (3, 3)
如果index
的长度不同,则会引发错误:
import pandas as pd
df1 = pd.DataFrame({'A':[1,2],
'B':[3,4],
'C':[5,6]}, index=['a','b'])
print (df1)
A B C
a 1 3 5
b 2 4 6
idx = pd.Index([5,6,7])
print (idx)
Int64Index([5, 6, 7], dtype='int64')
print (idx.shape)
(3,)
df2 = df1.values
print (df2)
[[1 3 5]
[2 4 6]]
print (df2.shape)
(2, 3)
print (pd.DataFrame(df2,index=idx))
ValueError: Shape of passed values is (3, 2), indices imply (3, 3)
答案 1 :(得分:0)
请提供示例数据集,因为一切都在这个简单的例子中起作用:
public void button1_Click(object sender, EventArgs e)
{
this.TopMost = true;
DialogResult result1 = MessageBox.Show("Add some notes to your current ticket?",
"Add Notes",
MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes)
{
Timer tm;
tm = new Timer();
int minutes = int.Parse(textBox2.Text);
tm.Interval = (int)TimeSpan.FromMinutes(minutes).TotalMilliseconds;
tm.Tick += new EventHandler(button1_Click);
tm.Enabled = true;
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + " ###";
Clipboard.SetText(pastebuffer);
tm.Start();
}
else if (result1 == DialogResult.No)
{
}
this.TopMost = false;
}