[英]How to split Python dataframe type float64 column into multiple columns
I need to run some calculations on some data pulled from a sales table using pyodbc. 我需要使用pyodbc对从sales表中提取的一些数据进行一些计算。 I am able to pull the data then I thought I would load it into a pandas dataframe.
我能够提取数据,然后我想我会把它加载到pandas数据帧中。 When the dataframe loads it has my data in one column when in reality it is 5 separate columns.
当数据框加载时,它将我的数据放在一列中,而实际上它是5个单独的列。
query = """SELECT OD.OrderNum, OD.Discount,OD.OrderQty,OD.UnitPrice, (a.OurReqQty - (a.OurJobShippedQty + a.OurStockShippedQty)) AS RemainingQty
FROM PUB.OrderDtl AS OD
INNER JOIN PUB.OrderRel AS a ON (OD.Company = a.Company) AND (OD.OrderNum = a.OrderNum) AND (OD.OrderLine = a.OrderLine)
WHERE (a.OpenRelease = 1)"""
print (query)
cnxn = pyodbc.connect(connection_string)
cursor = cnxn.cursor()
cursor.execute(query)
ab = list(cursor.fetchall())
df = pd.DataFrame(ab, columns=["remain"])
which returns this. 返回此。
[(115702, Decimal('0.00'), Decimal('25.00'), Decimal('145.00000'), Decimal('25.00')),
(115793, Decimal('0.00'), Decimal('20.00'), Decimal('823.00000'), Decimal('20.00')),
(115793, Decimal('0.00'), Decimal('20.00'), Decimal('823.00000'), Decimal('20.00')),
(116134, Decimal('0.00'), Decimal('10.00'), Decimal('587.00000'), Decimal('5.00')),
(116282, Decimal('0.00'), Decimal('1.00'), Decimal('699.95000'), Decimal('1.00'))]
When I load that into a dataframe it looks like this. 当我将其加载到数据框中时,它看起来像这样。
remain
0 [115702, 0.00, 25.00, 145.00000, 25.00]
1 [115793, 0.00, 20.00, 823.00000, 20.00]
2 [115793, 0.00, 20.00, 823.00000, 20.00]
3 [116134, 0.00, 10.00, 587.00000, 5.00]
4 [116282, 0.00, 1.00, 699.95000, 1.00]
I have tried to convert this to string by 我试图将其转换为字符串
df.index = df.index.map(str)
df_split = df["remain"].str.split(', ', 1)
But my split looks like 但我的分裂看起来像
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
I know this is a formatting issue or I assume it is but I don't know where to start. 我知道这是一个格式化问题,或者我认为它是,但我不知道从哪里开始。 I figured it would be easiest to split if it was a string but maybe I am missing something.
我认为如果它是一个字符串,最容易拆分,但也许我错过了一些东西。
thought this post would help but I think it requires me to export then reread the data back in. 认为这篇文章会有所帮助,但我认为它需要我导出然后重新读回数据。
I would greatly appreciate any help. 我非常感谢任何帮助。
Try this: 尝试这个:
col_names = ['OrderNum', 'Discount', 'OrderQty', 'UnitPrice', 'RemainingQty']
df_split = pd.DataFrame(df['remain'].values.tolist(), columns=col_names)
[out] [OUT]
OrderNum Discount OrderQty UnitPrice RemainingQty
0 115702 0.0 25.0 145.00 25.0
1 115793 0.0 20.0 823.00 20.0
2 115793 0.0 20.0 823.00 20.0
3 116134 0.0 10.0 587.00 5.0
4 116282 0.0 1.0 699.95 1.0
The behaviour you are seeing is due to the fact that .fetchall()
in pyodbc does not return a list of tuples, it returns a list of pyodbc.Row
objects. 您看到的行为是由于pyodbc中的
.fetchall()
没有返回元组列表,它返回一个pyodbc.Row
对象列表。
You should be able to fill your DataFrame directly by using pandas' read_sql method: 您应该可以使用pandas的read_sql方法直接填充DataFrame :
query = """\
SELECT OD.OrderNum,
OD.Discount,
OD.OrderQty,
OD.UnitPrice,
(a.OurReqQty - (a.OurJobShippedQty + a.OurStockShippedQty)) AS RemainingQty
FROM PUB.OrderDtl AS OD
INNER JOIN PUB.OrderRel AS a ON (OD.Company = a.Company)
AND (OD.OrderNum = a.OrderNum)
AND (OD.OrderLine = a.OrderLine)
WHERE (a.OpenRelease = 1)
"""
cnxn = pyodbc.connect(connection_string)
df = pd.read_sql(query, cnxn)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.