通过标签选择并写入csv

时间:2019-01-22 22:36:50

标签: python json pandas csv

我是熊猫新手,所以希望这是一个简单的解决方法。我正在尝试创建一个仅包含df.loc []中调用的列的csv文件。我的csv文件当前显示所有可用的行/列,因此选择不起作用。

import requests, json
import pandas as pd

# url and date set earlier
response = requests.post(url, date, headers={'Authorization':'OAuth mycode'})

if response.status_code == requests.codes.ok:
    response_json = response.json()
    print('\n\n\nReturned JSON\n\n\n')
    print(json.dumps(response_json, sort_keys=True, indent=4, separators=(',', ': ')))

    # df = pd.DataFrame.from_dict(response_json)
    df = pd.DataFrame(response_json)
    print(df.keys())
    df.loc[:,['Comment','EndTimeLocalized','Id','StartTimeLocalized']]
    df.to_csv("r1.csv")
    print('\nCSV Created\n')
else:
    response.raise_for_status()

2 个答案:

答案 0 :(得分:4)

pd.DataFrame.loc不是就地操作。

在导出前分配回df

df = df.loc[:, ['Comment','EndTimeLocalized','Id','StartTimeLocalized']]
df.to_csv('r1.csv')

或者,如果您希望保持df不变,请在之后立即切片并导出:

df.loc[:, ['Comment','EndTimeLocalized','Id','StartTimeLocalized']].to_csv('r1.csv')

答案 1 :(得分:0)

另一种选择是在对function Get-DiskFree { [CmdletBinding()] param ( [Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('cn')] [string[]]$ComputerName = $env:COMPUTERNAME, [Parameter(Position=1, Mandatory=$false)] [Alias('cr')] [System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty, [Parameter(Position=2)] [Alias('f')] [switch]$Format ) begin { $ErrorActionPreference = "Stop" function Format-HumanReadable { param ( $size ) switch ($size) { {$_ -ge 1PB} {"{0:#.#'P'}" -f ($size / 1PB); break} {$_ -ge 1TB} {"{0:#.#'T'}" -f ($size / 1TB); break} {$_ -ge 1GB} {"{0:#.#'G'}" -f ($size / 1GB); break} {$_ -ge 1MB} {"{0:#.#'M'}" -f ($size / 1MB); break} {$_ -ge 1KB} {"{0:#'K'}" -f ($size / 1KB); break} default {"{0}" -f ($size) + "B"} } } $wmiq = 'SELECT * FROM Win32_LogicalDisk WHERE Size != Null AND DriveType >= 2' } process { foreach ($computer in $ComputerName) { try { if ($computer -eq $env:COMPUTERNAME) { $disks = Get-CimInstance -Query $wmiq -ComputerName $computer } else { $disks = Invoke-Command -ArgumentList $wmiq { param($wmiq) Get-CimInstance -Query $wmiq } -ComputerName $computer -Credential $Credential ` | Select-Object DeviceID, DriveType, ProviderName, FreeSpace, Size, VolumeName } if ($Format) { # Create array for $disk objects and then populate $diskarray = @() $disks | ForEach-Object { $diskarray += $_ } $diskarray | Select-Object @{Name='Name'; Expression={$_.SystemName}}, @{Name='Vol'; Expression={$_.DeviceID}}, @{Name='Size'; Expression={Format-HumanReadable $_.Size}}, @{Name='Used'; Expression={Format-HumanReadable (($_.Size)-($_.FreeSpace))}}, @{Name='Avail'; Expression={Format-HumanReadable $_.FreeSpace}}, @{Name='Use%'; Expression={[int](((($_.Size)-($_.FreeSpace))/($_.Size) * 100))}}, @{Name='FS'; Expression={$_.FileSystem}}, @{Name='Type'; Expression={$_.Description}} } else { foreach ($disk in $disks) { $diskprops = @{ 'Volume'=$disk.DeviceID; 'Size'=$disk.Size; 'Used'=($disk.Size - $disk.FreeSpace); 'Available'=$disk.FreeSpace; 'FileSystem'=$disk.FileSystem; 'Type'=$disk.Description 'Computer'=$disk.SystemName; } # Create custom PS object and apply type $diskobj = New-Object -TypeName PSObject -Property $diskprops $diskobj.PSObject.TypeNames.Insert(0,'BinaryNature.DiskFree') Write-Output $diskobj } } } catch { # Check for common DCOM errors and display "friendly" output switch ($_) { { $_.Exception.ErrorCode -eq 0x800706ba } {$err = 'Unavailable (Host Offline or Firewall)'; break} { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } {$err = 'Access denied (Check User Permissions)'; break} default {$err = $_.Exception.Message} } Write-Warning "$computer - $err" } } } end { } } 的调用中将列指定为关键字参数:

to_csv