[英]when i transpose a dataframe, it shows nan values
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)
以下是output。 什么可能导致问题? 我不能将一个数据帧转换成另一个数据帧吗?
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)
我可以使用我的数据库重现 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 错误。 形状错误可能是由于您没有将“百分比”列传递给索引,但我无法重现此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.