【发布时间】:2021-08-05 10:19:04
【问题描述】:
如何创建一个函数来更改流光中数据框中列的 dtypes,其中用户选择他想要将所选列转换为它的类型?
我创建了一个将数据框作为参数的函数,而不是在下拉列表中显示用户可以从中选择的类型。
问题是当用户选择新类型并且系统打印df.info()时,所选列仍然具有旧类型。
代码:
import pandas as pd
import streamlit as st
def transform(df):
types = {'-':None
,'Boolean': '?'
,'Byte': 'b'
,'Integer':'i'
,'Floating point': 'f'
,'Date Time': 'M'
,'Time': 'm'
,'Unicode String':'U'
,'Object': 'O'}
new_types = {}
expander_types = st.beta_expander('Convert Data Types')
for i, col in enumerate(df.columns):
txt = 'Convert {} from {} to:'.format(col, df[col].dtypes)
expander_types.markdown(txt, unsafe_allow_html=True)
new_types[i] = expander_types.selectbox('Field to be converted:',[*types],index=0,key=i)
st.text(" \n") #break line
btn1 = st.button('Get CSV')
if btn1:
download_file(df, types, new_types, "csv")
print("transform",df.info())
我的函数的错误在哪里??
【问题讨论】: