简体   繁体   English

如何将Python数据框类型float64列拆分为多个列

[英]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.

相关问题 将包含 Float64 值的 1 个 DataFrame 列拆分为几个 - Split 1 DataFrame column containing Float64 values into several 将float64类型的DataFrame转换为在Python中浮动 - Convert float64 type DataFrame to float in Python 将 Panda Column dtype: float64 拆分为几列 - Split Panda Column dtype: float64 into several columns 在Python中二进制化float64 Pandas Dataframe - Binarize a float64 Pandas Dataframe in Python 如何找到float64中一列和另一列是对象(字符串)的两列pandas数据帧之间的相关性 - How to find correlation between two columns of pandas dataframe of one column in float64 and other column is object(string) 如何在python中将数据框中的一列拆分为多列? - How to split a column in a dataframe into multiple columns in python? Python Pandas:在包含数字和字符串元素的列中将类型从 float 更改为 float64 - Python Pandas: Change type from float to float64 in a column which contains both numerical and string elements Pandas DataFrame - 重复行并计算 float64 类型列的滚动平均值 - Pandas DataFrame - repeat rows and calculate rolling mean for column of type float64 提取字符或拆分列(dtype float64) - Extract characters or Split column (dtype float64) Python:如何将大数存储在 Pandas dataframe 作为 int64 或 float64? - Python: How to store large numbers in a Pandas dataframe as int64 or float64?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM