python - 将 Pandas DataFrame 转换为 CSV,但仅导出特定列
问题描述
我有一个熊猫数据框,它有 8 列需要写入 CSV,它工作得很好,但我只想将我的数据框的特定列转换为 csv。怎么做
from pandas import DataFrame
data = DataFrame.from_dict(diction)
data_to_write = data[["channel_id", "channel_img", "desc"]]`
{'data': [{'channel_id': 'UCqTFe5Dz3mpixUrLfMmY6dw', 'title': 'Manoyek',
'channel_img': 'https://yt3.ggpht.com/a-/AN66SAx-
JBOt1_NI6nXTBL2R95kDBaR6Spy9i4_q-g=s240-mo-c-c0xffffffff-rj-k-no', 'desc':
'Jestem Adrian' , 'subscriberCount_gained': 587372, 'subscriberCount_lost':
0}]}
解决方案
import pandas as pd
dictionary = {'data': [{'channel_id': 'UCqTFe5Dz3mpixUrLfMmY6dw', 'title': 'Manoyek', 'channel_img': 'https://yt3.ggpht.com/a-/AN66SAx-JBOt1_NI6nXTBL2R95kDBaR6Spy9i4_q-g=s240-mo-c-c0xffffffff-rj-k-no', 'desc': 'Jestem Adrian' , 'subscriberCount_gained': 587372, 'subscriberCount_lost': 0}]}
data = pd.DataFrame.from_dict(dictionary["data"])
data_to_write = data[["channel_id", "channel_img", "desc"]]
data_to_write.to_csv("filename.csv")
推荐阅读
- python - OperatorNotAllowedInGraphError:在图形执行中不允许使用 `tf.Tensor` 作为 Python `bool`。添加 Metrics 以编译函数
- azure - 仅来自某些 AppService 的 Azure 存储访问缓慢
- microsoft-teams - 如何拨打电话:适用于自适应卡
- java - 从选择对象设置选项
- apache-spark - java.lang.ClassNotFoundException:找不到类 com.amazon.ws.emr.hadoop.fs.EmrFileSystem
- java - 使用 downloadtask 下载多个 url
- java - 在某些按钮上随机放置文本
- java - REST API:服务中的字段存储库需要一个名为“entityManagerFactory”的 bean,但无法找到
- javascript - 如何使用“数据:文本/纯文本”打印井号?
- heroku - 在带有 .htpasswd 命令的 heroku CLI 中,输入的密码未显示