当我转置数据帧时,它显示nan值

2024-04-26 18:45:10 发布

您现在位置:Python中文网/ 问答频道 /正文

import pymysql
import pandas as pd
import numpy

conn = pymysql.connect(host="localhost",port=3306,db="school",user="root",password="@mit123")
print("Connection established sucessfully")
cursor = conn.cursor()

sql = "SELECT * FROM records"
cursor.execute(sql)
result = cursor.fetchall()

data= result
df = pd.DataFrame(data)
df1=df.T
print(df)
print(df1)

df2 = pd.DataFrame(df1,index=["id","name","rollno.","city"])
print(df2)

以下是输出。是什么导致了这个问题?我不能将一个数据帧转换成另一个数据帧吗

Connection established sucessfully
   0       1  2   3       4
0  1    amit  1  92  jorhat
1  2  subham  2  93  jorhat
2  3     ram  3  89   surat
3  4    anil  4  91   delhi
4  5   abdul  5  81  bhopal
5  6  joseph  6  90  sikkim
6  7     Ben  7  94  indore
7  8     tom  8  99     goa
        0       1      2      3       4       5       6    7
0       1       2      3      4       5       6       7    8
1    amit  subham    ram   anil   abdul  joseph     Ben  tom
2       1       2      3      4       5       6       7    8
3      92      93     89     91      81      90      94   99
4  jorhat  jorhat  surat  delhi  bhopal  sikkim  indore  goa
           0    1    2    3    4    5    6    7
id       NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
name     NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
rollno.  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
city     NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

Process finished with exit code 0

这是我的sql表:

此外,当我在数据框中使用索引时,它会显示形状错误:

Shape of passed values is (5, 8), indices imply (4, 8)

Tags: 数据importdfsqlnanconnectionconncursor
1条回答
网友
1楼 · 发布于 2024-04-26 18:45:10

我可以使用我的数据库重现NaN错误。所以我认为原因是那里没有列名。 因此,您可以执行以下操作:

import pymysql
import pandas as pd
import numpy

conn = pymysql.connect(host="localhost",
                       port=3306,
                       db="school",
                       user="root",
                       password="@mit123")

print("Connection established sucessfully")

sql = "SELECT * FROM records"

df = pd.read_sql(con=conn,sql=sql)

df1=df.T
print(df)
print(df1)

df2 = pd.DataFrame(df1,index=["id","name","roll_number","city"])
print(df2)

这解决了NaN错误。 形状错误可能是因为您没有将“百分比”列传递给索引,但我无法重现此错误

相关问题 更多 >