从for循环将多个值返回到数据框

时间:2018-07-19 08:53:24

标签: python pandas dataframe

classStyle

如何将我的代码中的ipaddress从0.0.0.0返回到x.x.x.x到字典中,以便我的数据框可以打印ipaddress。


我计划制作一个数据框,该数据框将为我提供

的输出
  • IPAddress
  • 域名
  • Lat&Long
  • 国家

我希望我的程序打印出从0.0.0.0到255.255.255.255的所有IP地址,但是要做到这一点,我需要拥有ipaddr() 返回多个值是否有办法呢?



我找到了解决问题的方法,如下所示:-

import pandas as pd
def makeaddr(a,b,c,d):
    return ipaddress.IPv4Address(bytes([a,b,c,d]))

def ipaddr():
    for x in range(256):
        a = makeaddr(x,x,x,x)
        a = str(a)
        return a

data = {'Ipaddress':[ipaddr() for x in range(256)]}
df = pd.DataFrame({key:pd.Series(value) for key, value in data.items() })
print(df)

如果有人可以减少LOC或改进此代码,请随时执行。

1 个答案:

答案 0 :(得分:0)

Create table #temp (ProductCode varchar(20), YTMAch int,  YTMTg int,  MTDTg int,  YTDPer int,  MTDPer int)

Insert into #temp values
('PrimaxX'  , 1, 2 , 3, 4, 5),
('SuperGrip', 1, 2 , 3, 4, 5),
('WC'       , 1, 2 , 3, 4, 5),
('WP'       , 1, 2 , 3, 4, 5)

Select id,
  max(case when ProductCode = 'PrimaxX' then value end) PrimaxX,
  max(case when ProductCode = 'SuperGrip' then value end) SuperGrip,
  max(case when ProductCode = 'WC' then value end) WC,
  max(case when ProductCode = 'WP' then value end) WP
from 
(Select * from #temp cross apply (values ('YTMAch',YTMAch), 
                                         ('YTMTg',YTMTg),   
                                         ('MTDTg',MTDTg),   
                                         ('YTDPer',YTDPer),  
                                         ('MTDPer',MTDPer))v(id,value))c 
group by id

我会对您的class IntArrayOracleType : UserType { override fun assemble(cached: Serializable?, owner: Any?) = deepCopy(cached) override fun deepCopy(value: Any?) = (anyToIntArraySafe(value))?.copyOf() override fun disassemble(value: Any?) = deepCopy(value) override fun equals(x: Any?, y: Any?) = (x?.equals(y) ?: y?.equals(x)) ?: true override fun hashCode(x: Any?) = x?.hashCode() ?: 0 override fun isMutable() = true override fun nullSafeGet(resultSet: ResultSet, names: Array<out String>?, session: SharedSessionContractImplementor?, owner: Any?): Any? { if (resultSet.wasNull() || names == null) { return null } return anyToIntArraySafe(resultSet.getArray(names[0])?.array) ?: intArrayOf() } override fun nullSafeSet(statement: PreparedStatement, value: Any?, index: Int, session: SharedSessionContractImplementor) { val connection = statement.connection if (value == null) { statement.setNull(index, Types.ARRAY, "INTEGER_VARRAY") } else { val oraConnection = connection.unwrap(OracleConnection::class.java) val array = oraConnection.createOracleArray("INTEGER_VARRAY", value) statement.setArray(index, array) } } override fun replace(original: Any?, target: Any?, owner: Any?) = (anyToIntArraySafe(original))?.copyOf() override fun returnedClass() = IntArray::class.java override fun sqlTypes() = intArrayOf(Types.ARRAY) } /** * Takes Any? and tries to cast it to Array and than to IntArray - BigDecimal is checked. * * Note that when given array contains anything else then BigDecimal or Int exception will be thrown * @return IntArray if successfully casted, null otherwise * */ internal fun anyToIntArraySafe(array: Any?) = (array as? IntArray) ?: (array as? Array<*>)?.map { it as? Int ?: (it as BigDecimal).intValueExact() }?.toIntArray() 函数进行一些更改,因为目前它返回的是标量而不是列表。

import pandas as pd
import random
import ipaddress

def makeaddr(ip):
    return ipaddress.IPv4Address(bytes(ip))

输出

ipaddr()

输出

def ipaddr():
    four_random_ints = [random.randint(0, 256) for x in range(4)]
    address = makeaddr(four_random_ints)
    return address

names = ['Tom', 'Jack', 'Steve', 'Ricky']

data = {'Names': names,
        'IP address':[ipaddr() for name in names]}